Unsolved enforcing minimum length of QLineEdit
-
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.
-
@mzimmers
So, setInputMask(“xxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX“);? -
@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.