PDA

View Full Version : QDialog check values



tskalex
7th September 2015, 13:57
Hi everyone,

I'm having a problem with QDialog. In my main() function I am creating an QDialog with multiple lines and checkBoxes where the user has to enter some information. Afterwards he can either click OK or Cancel.
What I want is: when clicking OK, the program should validate the entered information (e.g. if the necessary boxes were checked). If everything is fine, the information should be accepted and the dialog should close. If not, an error message should appear. After the error message the dialog is shown again with the wrong entered information. So, I don't want to just create a new dialog with empty fields every time, but I want to keep the entered information by the user.
When Cancel is clicked, the dialog should close.

I tried many things but none of them worked. I read about the done()-function, but I am not sure how to use it properly. I hope you can help me.

Thank you!

anda_skoa
7th September 2015, 14:05
The correct and very simple way to do this is to override the accept() method.

Just reimplement accept() in your dialog class and perform the validation there.
If you are satisfied you call the base class, i.e. QDialog::accept(), if you are not, just return

Cheers,
_

tskalex
7th September 2015, 14:18
Thank you for the very fast answer. I already tried that, but I have a problem. All the variables containing the user information are basically "stored" in the main()-function. But how can I access them if I am writing a new function like:

void QDialog::accept() {

...

}

I'm always getting an error when trying to access them the normal way.

Thanks!

anda_skoa
7th September 2015, 15:28
So you have information outside the dialog that you need for validating the user input inside the dialog?

Can't you simply pass it to the dialog before using it?

Cheers,
_

Radek
7th September 2015, 18:04
The dialog-set variables should belong to the dialog. If they have been set OK, pass them outside the dialog and call QDialog::accept(). If they aren't OK, show the error message and return from your accept().
How to "pass them outside" depends on your code. You are using some such method in fact because the "user information are basically stored in the main()-function".

Frdz
8th September 2015, 15:14
The following link will show you the example with code :
Validate-Data-in-QDialog (http://www.qtcentre.org/threads/8048-Validate-Data-in-QDialog)

Note :
# QDialog::Accepted & QDialog::done(r), don't change/remove "QDIALOG" with your dialog class name, or it will return to infinite loop. Just left it that way

^^