Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to enable a submit button after multiple form fields have been verified.
QtWS25 Last Chance

How to enable a submit button after multiple form fields have been verified.

Scheduled Pinned Locked Moved General and Desktop
13 Posts 7 Posters 10.5k 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on last edited by
    #3

    Yes, do as Volker suggests. This is a common pattern that you will likely use quite often.

    Nokia Certified Qt Specialist
    Interested in hearing about Qt related work

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vcsala
      wrote on last edited by
      #4

      The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #5

        I have seen this same question not too long ago on this forum. Not sure it was the same one asking though.

        While Volker's code is absolutely right, personally, I find something like this more readable:

        @
        void YourDialog::checkLineEdits()
        {
        bool ok(true);
        ok &= !ui->lineEdit1->text().isEmpty();
        ok &= !ui->lineEdit2->text().isEmpty();
        ok &= !ui->lineEdit3->text().isEmpty();
        ok &= !ui->lineEdit4->text().isEmpty();

        ui->okButton->setEnabled(ok);
        

        }
        @

        And I know people here are going to disagree with me. :-)
        Of course, variations are possible too, for instance one where you iterate over all QLineEdits (no matter how many there are):

        @
        void YourDialog::checkLineEdits()
        {
        bool ok(true);
        QList<QLineEdit*> lineEditList = findChildren<QLineEdit*>();
        foreach (QLineEdit* le, lineEditList)
        ok &= !le->text().isEmpty();

        ui->okButton->setEnabled(ok);
        

        }
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #6

          It makes little sense to use bitwise operators on a bool, doesn't it? :P

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #7

            [quote author="peppe" date="1302002385"]It makes little sense to use bitwise operators on a bool, doesn't it? :P[/quote]

            How would you do then? On linux you can use '*=' instead of '&=' if both operands are bool, but I was told by an expert that (at least some) windows compiler just cannot stand such a notation.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #8

              I was just punning on:
              [quote]And I know people here are going to disagree with me. :-)[/quote]

              In C unfortunately there's no short-circuiting operator &&= (which would be required in this case).

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #9

                [quote author="peppe" date="1302002385"]It makes little sense to use bitwise operators on a bool, doesn't it? :P[/quote]

                Ah, well, they just are very short. Just one bit to compare bitwise. It works nonetheless.

                If you want to do it "right", I guess you'd have to do this:
                @
                void YourDialog::checkLineEdits()
                {
                bool ok(true);
                ok = ok && !ui->lineEdit1->text().isEmpty();
                ok = ok && !ui->lineEdit2->text().isEmpty();
                ok = ok && !ui->lineEdit3->text().isEmpty();
                ok = ok && !ui->lineEdit4->text().isEmpty();

                ui->okButton->setEnabled(ok);
                

                }
                @

                Does that get more readable? Not really, I think.

                P.S. I was expecting comments on this not being efficient, actually. :-)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #10

                  [quote author="VCsala" date="1301955029"]The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.[/quote]

                  There is no state machine involved. The code always checks all four line edits. There may be some short circuiting by the compiler, if the first check is false, the complete expression is false, so there is no need to calculate the rest.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vcsala
                    wrote on last edited by
                    #11

                    [quote author="Volker" date="1302021381"][quote author="VCsala" date="1301955029"]The method Volker suggested is the safe way to do it and as I understand it uses some kind of state machine for storing the actual state of validity therefore it does not cause significant overhead even if your validation is resource hungry.[/quote]

                    There is no state machine involved. The code always checks all four line edits. There may be some short circuiting by the compiler, if the first check is false, the complete expression is false, so there is no need to calculate the rest.[/quote]

                    However, if you want to have more complex check on the input field (and not only checking if it is empty or not) and you use validator (which is a more generic solution for this use case) then you have validity states (and in this case the validation itself can be resource and time consuming). As I know even in this case the above pattern is safe as the states are recalculated only if the field has been changed.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mchris357
                      wrote on last edited by
                      #12

                      Thanks for all the input! QT certainly has an active AND helpful community...something that can be difficult to find in combination.

                      1 Reply Last reply
                      0
                      • Z Offline
                        Z Offline
                        ZapB
                        wrote on last edited by
                        #13

                        Don't forget good looking too ;-)

                        Good luck with your project!

                        Nokia Certified Qt Specialist
                        Interested in hearing about Qt related work

                        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