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