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. Disable Dialog OK button until input valid
Forum Update on Monday, May 27th 2025

Disable Dialog OK button until input valid

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 3.3k Views
  • 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.
  • B Offline
    B Offline
    Bob64
    wrote on 11 Jul 2019, 19:33 last edited by
    #1

    I am writing what I assumed to be a simple dialog, using Dialog. This holds a couple of input fields that need to be provided by the user before they should be allowed to click OK. I have got the layout how I want it and so on and now I find I am stuck in trying to add the logic to disable the button. Perhaps I am naive but I assumed that this would be straightforward. Am I missing something?

    In fact it seems that every time I have tried to use Dialog, I have hit some sort of issue. Since starting to write this question I thought to go back and look at some other things I have done and it turns out that I implemented something similar previously using Window and doing all my own button handling logic and layout. Is that the correct way to do it or is there a simpler option staring me in the face?

    J 1 Reply Last reply 11 Jul 2019, 20:47
    0
    • B Offline
      B Offline
      Bob64
      wrote on 12 Jul 2019, 09:17 last edited by Bob64 7 Dec 2019, 09:27
      #4

      OK, apologies - I am still quite new to QML. Some things have clicked with me and others still seem to feel more difficult than I think they should.

      I should have been more explicit. I understand the general paradigm but what was not obvious was how actually to get hold of the buttons to disable/enable them? ( @JonB you seem to be talking about Qt widgets - this is the QML sub-forum. As I say, I understand the principles and that is the sort of thing I would have done many years ago when I used to use Qt widgets.)

      To answer my own question, it seems that I need to explicitly assign a DialogButtonBox as the footer. I can then reference the standardButton method on the DialogButtonBox. To me this is an example of things just being a bit more inconvenient than they should be and involves a certain amount of reading between the lines in the documentation.

      For example:

      import QtQuick 2.9
      import QtQuick.Controls 2.2
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 200
          height: 200
          title: qsTr("Hello World")
      
          Dialog {
              id: dlg
              modal: true
      
              height: parent.height
              width: parent.width
      
              TextField {
                  anchors.centerIn: parent
                  height: 30
                  width: 60
                  validator: IntValidator {}
                  onTextEdited: {
                      buttons.standardButton(Dialog.Ok).enabled = acceptableInput;
                  }
              }
              
              footer: DialogButtonBox {
                  id: buttons
                  standardButtons: Dialog.Ok | Dialog.Cancel
              }
      
              onOpened: {
                  buttons.standardButton(Dialog.Ok).enabled = false;
              }
          }
      
          Component.onCompleted: {
              dlg.open();
          }
      }
      
      J 1 Reply Last reply 12 Jul 2019, 12:20
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Jul 2019, 20:14 last edited by
        #2

        Hi,

        The usual way is to react on each modification of an input and then check if they are all provided and valid and only then enable the button.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply 16 Jun 2024, 07:05
        1
        • B Bob64
          11 Jul 2019, 19:33

          I am writing what I assumed to be a simple dialog, using Dialog. This holds a couple of input fields that need to be provided by the user before they should be allowed to click OK. I have got the layout how I want it and so on and now I find I am stuck in trying to add the logic to disable the button. Perhaps I am naive but I assumed that this would be straightforward. Am I missing something?

          In fact it seems that every time I have tried to use Dialog, I have hit some sort of issue. Since starting to write this question I thought to go back and look at some other things I have done and it turns out that I implemented something similar previously using Window and doing all my own button handling logic and layout. Is that the correct way to do it or is there a simpler option staring me in the face?

          J Offline
          J Offline
          JonB
          wrote on 11 Jul 2019, 20:47 last edited by JonB 7 Nov 2019, 20:48
          #3

          @Bob64
          Exactly as @SGaist has just said. You don't have to implement your own anything-complicated. No Dialog vs Window issue.

          You start with the button disabled. If your "input fields" are QLineEdits, say, you will respond to their textChanged() signal by looking to see if they are now "acceptable", and when they all are you enable the button.

          It is straightforward! :)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bob64
            wrote on 12 Jul 2019, 09:17 last edited by Bob64 7 Dec 2019, 09:27
            #4

            OK, apologies - I am still quite new to QML. Some things have clicked with me and others still seem to feel more difficult than I think they should.

            I should have been more explicit. I understand the general paradigm but what was not obvious was how actually to get hold of the buttons to disable/enable them? ( @JonB you seem to be talking about Qt widgets - this is the QML sub-forum. As I say, I understand the principles and that is the sort of thing I would have done many years ago when I used to use Qt widgets.)

            To answer my own question, it seems that I need to explicitly assign a DialogButtonBox as the footer. I can then reference the standardButton method on the DialogButtonBox. To me this is an example of things just being a bit more inconvenient than they should be and involves a certain amount of reading between the lines in the documentation.

            For example:

            import QtQuick 2.9
            import QtQuick.Controls 2.2
            import QtQuick.Window 2.2
            
            Window {
                visible: true
                width: 200
                height: 200
                title: qsTr("Hello World")
            
                Dialog {
                    id: dlg
                    modal: true
            
                    height: parent.height
                    width: parent.width
            
                    TextField {
                        anchors.centerIn: parent
                        height: 30
                        width: 60
                        validator: IntValidator {}
                        onTextEdited: {
                            buttons.standardButton(Dialog.Ok).enabled = acceptableInput;
                        }
                    }
                    
                    footer: DialogButtonBox {
                        id: buttons
                        standardButtons: Dialog.Ok | Dialog.Cancel
                    }
            
                    onOpened: {
                        buttons.standardButton(Dialog.Ok).enabled = false;
                    }
                }
            
                Component.onCompleted: {
                    dlg.open();
                }
            }
            
            J 1 Reply Last reply 12 Jul 2019, 12:20
            1
            • B Bob64
              12 Jul 2019, 09:17

              OK, apologies - I am still quite new to QML. Some things have clicked with me and others still seem to feel more difficult than I think they should.

              I should have been more explicit. I understand the general paradigm but what was not obvious was how actually to get hold of the buttons to disable/enable them? ( @JonB you seem to be talking about Qt widgets - this is the QML sub-forum. As I say, I understand the principles and that is the sort of thing I would have done many years ago when I used to use Qt widgets.)

              To answer my own question, it seems that I need to explicitly assign a DialogButtonBox as the footer. I can then reference the standardButton method on the DialogButtonBox. To me this is an example of things just being a bit more inconvenient than they should be and involves a certain amount of reading between the lines in the documentation.

              For example:

              import QtQuick 2.9
              import QtQuick.Controls 2.2
              import QtQuick.Window 2.2
              
              Window {
                  visible: true
                  width: 200
                  height: 200
                  title: qsTr("Hello World")
              
                  Dialog {
                      id: dlg
                      modal: true
              
                      height: parent.height
                      width: parent.width
              
                      TextField {
                          anchors.centerIn: parent
                          height: 30
                          width: 60
                          validator: IntValidator {}
                          onTextEdited: {
                              buttons.standardButton(Dialog.Ok).enabled = acceptableInput;
                          }
                      }
                      
                      footer: DialogButtonBox {
                          id: buttons
                          standardButtons: Dialog.Ok | Dialog.Cancel
                      }
              
                      onOpened: {
                          buttons.standardButton(Dialog.Ok).enabled = false;
                      }
                  }
              
                  Component.onCompleted: {
                      dlg.open();
                  }
              }
              
              J Offline
              J Offline
              JonB
              wrote on 12 Jul 2019, 12:20 last edited by JonB 7 Dec 2019, 12:47
              #5

              @Bob64

              @JonB you seem to be talking about Qt widgets - this is the QML sub-forum.

              You are quite correct! Sorry, I sit in "Unread" category, I never seem to notice which forum new posts are in.

              1 Reply Last reply
              0
              • S SGaist
                11 Jul 2019, 20:14

                Hi,

                The usual way is to react on each modification of an input and then check if they are all provided and valid and only then enable the button.

                N Offline
                N Offline
                nenchev
                wrote on 16 Jun 2024, 07:05 last edited by nenchev
                #6

                @SGaist This seems like a workaround, rather than a solution, this is usually a bad way to do user-input validation. The assumption that you can just react to text change and enable/disable the button in all cases is shortsighted. What if I want to call a web service to validate, or perform some other time consuming or expensive operation?

                S 1 Reply Last reply 16 Jun 2024, 18:43
                0
                • N nenchev
                  16 Jun 2024, 07:05

                  @SGaist This seems like a workaround, rather than a solution, this is usually a bad way to do user-input validation. The assumption that you can just react to text change and enable/disable the button in all cases is shortsighted. What if I want to call a web service to validate, or perform some other time consuming or expensive operation?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 16 Jun 2024, 18:43 last edited by
                  #7

                  @nenchev then you implement it differently.

                  My suggestion is not a solution applicable to all circonstances as there are no such things. If your validation takes that long to process then you have to give your user a better feedback to let them know longer processing is required and ongoing but it also does not makes sens to add a progress bar if your validation takes less than a second.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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