Reply
Thread Tools
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#141
Hi again guys.

I need some help with validating user input and then do actions based on if the input is valid or not.

So far I'm good with validating the input, the validator allows me only to input correctly formatted stuff. What it doesn't check is if the input length is enough. What I don't know is how to know later if the input was long enough or not.

Here's what I do when program launches (I create the validator and set it to line edits):
Code:
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    progress(tr("Setting up the theme, please wait..."), tr("Abort"), 0, 0, this),
    progress2(tr("Searching and importing fonts, please wait..."), tr("Abort"), 0, 0, this),
    colors()
{
    ui->setupUi(this);
    ui->scrollArea->setWidget(ui->widget);
    readdirs();
    oldfonts();
    readSettings();
    QRegExp regexer("\\#[0-9a-fA-F]{6,6}");
    QValidator* actualvalidator;
    actualvalidator = new QRegExpValidator(regexer, this);
    //QRegExpValidator validator();
    ui->line_edit_font1->setValidator(actualvalidator);
    ui->line_edit_font2->setValidator(actualvalidator);
    ui->line_edit_font3->setValidator(actualvalidator);
    ui->line_edit_font4->setValidator(actualvalidator);
    ui->line_edit_font5->setValidator(actualvalidator);
    ui->line_edit_font6->setValidator(actualvalidator);
}
How can I check in some slot later if the input is valid and if it's not, then do some actions. Like:
Code:
void MainWindow::on_line_edit_font1_editingFinished()
{

if ( user inputted seven characters, which is valid ) {

// do something with the valid input

} elseif ( user inputted only four characters which is INVALID ) {

// tell user he's bad and he should write only valid things...

}

}
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE

Last edited by d-iivil; 2010-09-30 at 10:11.
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#142
Originally Posted by D-Iivil View Post
Code:
void MainWindow::on_line_edit_font1_editingFinished()
{

if ( user inputted seven characters, which is valid ) {

// do something with the valid input

} elseif ( user inputted only four characters which is INVALID ) {

// tell user he's bad and he should write only valid things...

}

}

Code:
if (ui->line_edit_font1->text().length() == 7) {

// do something with the valid input

} elseif (ui->line_edit_font1->text().length() == 4) {

// tell user he's bad and he should write only valid things...

}
But how can the user enter invalid short text?
I don't know how this qtvalidator work, but isn't that
enough to prevent the short text?
regards
Nicolai
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#143
Originally Posted by nicolai View Post
Code:
if (ui->line_edit_font1->text().length() == 7) {

// do something with the valid input

} elseif (ui->line_edit_font1->text().length() == 4) {

// tell user he's bad and he should write only valid things...

}
But how can the user enter invalid short text?
I don't know how this qtvalidator work, but isn't that
enough to prevent the short text?
regards
Nicolai
Apparently it's not enough. I can type in only three letters and move on to next box.

What I want to achieve is that when user has typed in less than seven digits and he jumps to next box, it would revert the box value back to what it was before user started to type the stuff in the box.

That way I could prevent situation where the box has less than seven digits in it.
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
Berserk's Avatar
Posts: 199 | Thanked: 156 times | Joined on May 2010 @ Holland
#144
Originally Posted by D-Iivil View Post
Code:
// tell user he's bad


Originally Posted by D-Iivil View Post
Code:
QRegExp regexer("\\#[0-9a-fA-F]{6,6}");
I think you need to make it this way:
Code:
QRegExp regexer("^\\#[0-9a-fA-F]{6,6}$");
^ = from the start of the string
(.. your characters ..)
$ = until the end of the string

Also, you might consider this:
Code:
ui->line_edit_font1->setValidator(new QRegExpValidator(regexer, ui->line_edit_font1);
I think the parent is wrong, you have it on "this", which is the whole class, I think you need to put it on the QLineEdits specifically.
Edit: this way, you don't have to make "actualvalidator".
__________________
Wallpaeper - Desktop background manager (in Extras!)
Config Reader - Export all Gconf to an HTML file + compare feature (Extras-testing)
Even though these programs are available for free, I would appreciate any donations

Last edited by Berserk; 2010-09-30 at 10:47.
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#145
Here's a screenshot of situation I wan't to avoid:
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#146
Originally Posted by Berserk View Post


I think you need to make it this way:
Code:
QRegExp regexer("^\\#[0-9a-fA-F]{6,6}$");
^ = from the start of the string
(.. your characters ..)
$ = until the end of the string

Also, you might consider this:
Code:
ui->line_edit_font1->setValidator(new QRegExpValidator(regexer, ui->line_edit_font1);
I think the parent is wrong, you have it on "this", which is the whole class, I think you need to put it on the QLineEdits specifically.
Edit: this way, you don't have to make "actualvalidator".
Didn't work. I can still type in only two or three characters and jump on editing next box. However while I do that the signal editingFinished ins't triggered if I don't fill in all seven digits.

So maybe I need to do something with the validator? Tell validator to fill in the previous value if user didn't fill in all required digits? How?
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#147
Originally Posted by Berserk View Post


I think you need to make it this way:
Code:
QRegExp regexer("^\\#[0-9a-fA-F]{6,6}$");
^ = from the start of the string
(.. your characters ..)
$ = until the end of the string
No. From the Qt-Doc about QtRegExpValidator:
... The match is made against the entire string, e.g. if the regexp is [A-Fa-f0-9]+ it will be treated as ^[A-Fa-f0-9]+$.
 
Posts: 180 | Thanked: 76 times | Joined on May 2010
#148
Maybe you can use void QLineEdit::editingFinished () signal.

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.

The input is valid if the signal is emitted.
 
d-iivil's Avatar
Posts: 2,154 | Thanked: 2,186 times | Joined on Dec 2009 @ Hellsinki, Finland
#149
Originally Posted by Diph View Post
Maybe you can use void QLineEdit::editingFinished () signal.

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable.

The input is valid if the signal is emitted.
That's the case... signal isn't emitted if I fill in only three digits (or four, or five), but on the screen I see the three digits. I want it to revert back to the value that was in the box before I started editing it and didn't complete the input.
__________________
If you're rich and you think I deserve a cold beer, you may donate one or two :-P

80's style stadium rock is back - FIRENOTE
Hi-Octane heavy metal - FORCE MAJEURE
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#150
Is the behavrio different, if you use setInputMask instead
of a validator?

http://doc.trolltech.com/4.7/qlineed...inputMask-prop

ui->line_edit_font1->setInputMask("\#NNNNNN");

regards
Nicolai
 
Reply


 
Forum Jump


All times are GMT. The time now is 17:55.