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. enforcing minimum length of QLineEdit

enforcing minimum length of QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 5.1k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I have a window with QLineEdits for WPA SSID and PSK fields. They must be a minimum of 6 and 8 characters, respectively. I'd like to enforce this at the editing level, ideally, but I'm not sure how to go about it. I can't figure out how to code a validator for this -- it can return Intermediate if the input is too short, but I don't know what to do with this result.

    I also tried using an input mask "NNNNNN" (for SSID) but that results in inconvenient editing.

    Any suggestions/tips/tricks I should know about? Thanks.

    J.HilkJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      I have a window with QLineEdits for WPA SSID and PSK fields. They must be a minimum of 6 and 8 characters, respectively. I'd like to enforce this at the editing level, ideally, but I'm not sure how to go about it. I can't figure out how to code a validator for this -- it can return Intermediate if the input is too short, but I don't know what to do with this result.

      I also tried using an input mask "NNNNNN" (for SSID) but that results in inconvenient editing.

      Any suggestions/tips/tricks I should know about? Thanks.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      Hi @mzimmers

      The easiest would be, setting an input mask + eventually a regexpression validator, if you want to restrict the characters/numbers at specific points.

      ——
      Edit: Docu-Link


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Just as a note. Sometimes edit masks make editing clunky so
        its you have any sort of OK button , you could check the length
        and inform user if its not ok when button pressed.
        But it depends what you need to filter. If you need to disallow
        some letters etc then as @J-Hilk says is clearly the way to go.

        1 Reply Last reply
        0
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          I try to avoid regex wherever possible, and it doesn't seem necessary here. I don't need to disallow any printing characters. I've looked for rules on what makes a valid SSID and PSK in WPA2-land, and all I could come up with is:

          • SSID from 6 to 32 characters
          • PSK from 8 to 64 characters
          • all characters must be 0x20 to 0x126.

          My "commit" button is for the entire list of entries, not individual fields. I guess I could just check the length when editingFinished() is raised, and put up an error box?

          mrjjM J.HilkJ 2 Replies Last reply
          0
          • mzimmersM mzimmers

            I try to avoid regex wherever possible, and it doesn't seem necessary here. I don't need to disallow any printing characters. I've looked for rules on what makes a valid SSID and PSK in WPA2-land, and all I could come up with is:

            • SSID from 6 to 32 characters
            • PSK from 8 to 64 characters
            • all characters must be 0x20 to 0x126.

            My "commit" button is for the entire list of entries, not individual fields. I guess I could just check the length when editingFinished() is raised, and put up an error box?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mzimmers
            Hi
            Well i prefer to color the border red instead of pop up anything but yes
            a error box would also work. :)

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              If your users are knowledgeable enough to know specifically what a red outline means, I agree that's a better way to go. My app has to be geared to people who aren't so sophisticated, so a cumbersome error box seems the best way to go.

              mrjjM 1 Reply Last reply
              0
              • mzimmersM mzimmers

                I try to avoid regex wherever possible, and it doesn't seem necessary here. I don't need to disallow any printing characters. I've looked for rules on what makes a valid SSID and PSK in WPA2-land, and all I could come up with is:

                • SSID from 6 to 32 characters
                • PSK from 8 to 64 characters
                • all characters must be 0x20 to 0x126.

                My "commit" button is for the entire list of entries, not individual fields. I guess I could just check the length when editingFinished() is raised, and put up an error box?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @mzimmers
                So, setInputMask(“xxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX“);?


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                mzimmersM 1 Reply Last reply
                2
                • mzimmersM mzimmers

                  If your users are knowledgeable enough to know specifically what a red outline means, I agree that's a better way to go. My app has to be geared to people who aren't so sophisticated, so a cumbersome error box seems the best way to go.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @mzimmers said in enforcing minimum length of QLineEdit:

                  If your users are knowledgeable enough to know specifically what a red outline means,

                  Well its a tried solution for most web forms. so most user respond
                  very well to it if its clear whats wrong.
                  However, the msg box is easier to implement and u can directly tell "too short. must be X"

                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @mzimmers
                    So, setInputMask(“xxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX“);?

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #9

                    @J.Hilk that would be the best mask, but I just don't like how the line editing works with it enabled. Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position). I don't know whether this is doable or not.

                    J.HilkJ JonBJ 2 Replies Last reply
                    0
                    • mzimmersM mzimmers

                      @J.Hilk that would be the best mask, but I just don't like how the line editing works with it enabled. Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position). I don't know whether this is doable or not.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @mzimmers said in enforcing minimum length of QLineEdit:

                      Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position). I don't know whether this is doable or not.

                      I don‘t know if it‘s still requiered to do so, but I rememberung subclassing QlineEdit, and setting the curser position during the enterEvent


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @J.Hilk that would be the best mask, but I just don't like how the line editing works with it enabled. Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position). I don't know whether this is doable or not.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @mzimmers

                        Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position).

                        I don't understand. What is about your situation which makes you want to alter whatever the default behaviour is? Why should the cursor go to the start? Plus, this bit has nothing to do with the input mask, or does it?

                        mzimmersM 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @mzimmers

                          Specifically, it seems that when the user clicks anywhere in the field, the cursor should go to the start (first character position).

                          I don't understand. What is about your situation which makes you want to alter whatever the default behaviour is? Why should the cursor go to the start? Plus, this bit has nothing to do with the input mask, or does it?

                          mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #12

                          @JonB I could be all wet on this, but to my way of thinking, the mere requirement of a minimum length for a field should not imply that the field isn't blank when the display initializes. When I use a mask such as suggested by J.Hilk, the field doesn't "behave" as though it's blank; it behaves as though it's filled with spaces that magically disappear when you begin entering other characters.

                          I'm not aware of this behavior on any UI I've ever used, and I would find it confusing. It could well be that I'm not using the tools properly -- obviously I'm no expert in this -- but no matter the cause, the result just isn't satisfactory.

                          JonBJ 1 Reply Last reply
                          0
                          • mzimmersM mzimmers

                            @JonB I could be all wet on this, but to my way of thinking, the mere requirement of a minimum length for a field should not imply that the field isn't blank when the display initializes. When I use a mask such as suggested by J.Hilk, the field doesn't "behave" as though it's blank; it behaves as though it's filled with spaces that magically disappear when you begin entering other characters.

                            I'm not aware of this behavior on any UI I've ever used, and I would find it confusing. It could well be that I'm not using the tools properly -- obviously I'm no expert in this -- but no matter the cause, the result just isn't satisfactory.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @mzimmers
                            OK, I didn't know it behaved like that.

                            I found the character-by-character validation of Qt problematic elsewhere. Going back to your original question. You don't like regular expressions or masks, so why don't you just check the length. You return "intermediate" while it's less than 6/8 characters, that lets the user keep typing you don't have to do anything, and return "error" at 7/9, that stops the user typing any more.

                            mzimmersM 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @mzimmers
                              OK, I didn't know it behaved like that.

                              I found the character-by-character validation of Qt problematic elsewhere. Going back to your original question. You don't like regular expressions or masks, so why don't you just check the length. You return "intermediate" while it's less than 6/8 characters, that lets the user keep typing you don't have to do anything, and return "error" at 7/9, that stops the user typing any more.

                              mzimmersM Offline
                              mzimmersM Offline
                              mzimmers
                              wrote on last edited by
                              #14

                              @Jon I don't think that will give me an opportunity to give the user an error message when he tries to enter a string of invalid length.

                              The right approach here seems to be to confine the validator to making sure that only printable characters are entered, and not length check. When the editingFinished() signal is invoked, I can length check then.

                              It would be nice to have the cursor returned to the invalid field. I tried using the setFocus() method on the field, but this gives undesired results when trying to cancel out or close the window while invalid input is in the field. Here's what I've tried:

                              QObject::connect(ui->lineEditSSID, &QLineEdit::editingFinished, this, &WifiSetup::checkSsidLength);
                              ...
                              void WifiSetup::checkSsidLength()
                              {
                                  if (ui->lineEditSSID->text().length() < MIN_SSID_LEN ||
                                          ui->lineEditSSID->text().length() > MAX_SSID_LEN)
                                  {
                                      QMessageBox msgBox;
                                      QString qs;
                                      qs.append("The entered SSID must be between ");
                                      qs.append(tr("%1").arg(MIN_SSID_LEN));
                                      qs.append(" and ");
                                      qs.append(tr("%1").arg(MAX_SSID_LEN));
                                      qs.append(" characters in length.");
                                      msgBox.setText(qs);
                                      msgBox.exec();
                                      ui->lineEditSSID->setFocus();
                                  }
                              }
                              

                              It seems I need to ignore the signal when the "cancel" button is pushed. I tried using the "isDown()" method, but this only returns true after the initial signal, which is too late. Any other suggestions?

                              JonBJ 1 Reply Last reply
                              0
                              • mzimmersM mzimmers

                                @Jon I don't think that will give me an opportunity to give the user an error message when he tries to enter a string of invalid length.

                                The right approach here seems to be to confine the validator to making sure that only printable characters are entered, and not length check. When the editingFinished() signal is invoked, I can length check then.

                                It would be nice to have the cursor returned to the invalid field. I tried using the setFocus() method on the field, but this gives undesired results when trying to cancel out or close the window while invalid input is in the field. Here's what I've tried:

                                QObject::connect(ui->lineEditSSID, &QLineEdit::editingFinished, this, &WifiSetup::checkSsidLength);
                                ...
                                void WifiSetup::checkSsidLength()
                                {
                                    if (ui->lineEditSSID->text().length() < MIN_SSID_LEN ||
                                            ui->lineEditSSID->text().length() > MAX_SSID_LEN)
                                    {
                                        QMessageBox msgBox;
                                        QString qs;
                                        qs.append("The entered SSID must be between ");
                                        qs.append(tr("%1").arg(MIN_SSID_LEN));
                                        qs.append(" and ");
                                        qs.append(tr("%1").arg(MAX_SSID_LEN));
                                        qs.append(" characters in length.");
                                        msgBox.setText(qs);
                                        msgBox.exec();
                                        ui->lineEditSSID->setFocus();
                                    }
                                }
                                

                                It seems I need to ignore the signal when the "cancel" button is pushed. I tried using the "isDown()" method, but this only returns true after the initial signal, which is too late. Any other suggestions?

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @mzimmers

                                @Jon I don't think that will give me an opportunity to give the user an error message when he tries to enter a string of invalid length.

                                It would prevent (stop the user typing) if they tried to type too many characters into field. You still get/have to check & issue the error for too few characters on move off field. Seems pretty normal to me.

                                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