Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Conditionally closing a dialog
Forum Updated to NodeBB v4.3 + New Features

Conditionally closing a dialog

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
dialoginputacceptqmloverride
2 Posts 2 Posters 402 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LorenDB
    wrote on 8 Jul 2021, 17:35 last edited by
    #1

    I'm writing a program that includes a dialog that requires some user input, like so:

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtQuick.Layouts 1.12
    
    Dialog {
        id: foobar
    
        function show() {
            input.text = ""
            input.forceActiveFocus(Qt.PopupFocusReason)
            open()
        }
    
        title: "Foobar"
        modal: true
        width: 350
        standardButtons: Dialog.Ok | Dialog.Cancel
    
        onAccepted: console.log("foobar: ", input.text)
    
        contentItem: RowLayout {
            spacing: 10
    
            Label {
                text: "Number of pages:"
            }
    
            TextField {
                id: input
    
                validator: IntValidator {}
                placeholderText: "1234"
                Layout.fillWidth: true
            }
        }
    }
    

    Without having to implement a custom footer such as a DialogButtonBox, I'd like to override the accepted() signal to only continue being accepted if the user has put something in input.text. If input.text is null, I want to stop the dialog from closing.

    Apologies if I'm not making myself clear; I will be happy to provide any details necessary. I'm using Qt 5.15.2 on Linux.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      e1985t
      wrote on 22 Jul 2024, 08:45 last edited by
      #2

      I also needed this but there was no answer for it anywhere.
      Here is my solution:

          Dialog {
              id: loginDialog
              title: "Login"
              modal: true
      
              LoginDialog {
                  id: loginPage
                  anchors.fill: parent
              }
      
              footer: DialogButtonBox {
                  id: buttons
                  Button {
                      id: okButton
                      text: qsTr("Ok")
                      DialogButtonBox.buttonRole: DialogButtonBox.InvalidRole
      
                      Connections {
                          target: okButton
                          function onClicked() {
                              if (CONDITION){
                                  loginDialog.accept()
                              } else {
                              }
                          }
                      }
                  }
                  Button {
                      id: cancelButton
                      text: qsTr("Cancel")
                      DialogButtonBox.buttonRole: DialogButtonBox.RejectRole
                  }
              }
          }
      

      So my answer is replace standard buttons and handle button clicks yourself.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved