maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Need assistance from QT programmer (https://talk.maemo.org/showthread.php?t=58569)

d-iivil 2010-09-30 10:08

Re: Need assistance from QT programmer
 
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...

}

}


nicolai 2010-09-30 10:35

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by D-Iivil (Post 829954)
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 2010-09-30 10:39

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by nicolai (Post 829975)
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.

Berserk 2010-09-30 10:43

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by D-Iivil (Post 829954)
Code:

// tell user he's bad

:D

Quote:

Originally Posted by D-Iivil (Post 829954)
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".

d-iivil 2010-09-30 10:43

Re: Need assistance from QT programmer
 
Here's a screenshot of situation I wan't to avoid:
http://i4.aijaa.com/b/00298/6799969.png

d-iivil 2010-09-30 10:49

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Berserk (Post 829981)
:D

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?

nicolai 2010-09-30 10:58

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Berserk (Post 829981)
:D

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:
Quote:

... 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]+$.

Diph 2010-09-30 10:58

Re: Need assistance from QT programmer
 
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 2010-09-30 11:00

Re: Need assistance from QT programmer
 
Quote:

Originally Posted by Diph (Post 829995)
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.

nicolai 2010-09-30 11:04

Re: Need assistance from QT programmer
 
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


All times are GMT. The time now is 23:14.

vBulletin® Version 3.8.8