PDA

View Full Version : QMessageBox If not enter all the variables



NewLegend
18th November 2010, 11:35
Hello
I want to make a warning message when the user does not enter all the variables??

I have used this method, But it did not work.

try
{
bool ok;
int id = ui->lineEdit->text().toInt(&ok,10);
int Name= ui->lineEdit_2->text().toInt(&ok,10);
int M_Case = ui->lineEdit_3->text().toInt(&ok,10);
}
catch (std::exception& e) {
QMessageBox::critical(NULL, QString("Error"),QString("You must enter all the variables id, Name and M_Case "));
}

FlashMuller
18th November 2010, 11:49
Here's what I did when I had that Problem:
As you're programming with Qt I suppose your input is being done by QLineEdit or QTextEdit. You can check whether they are empty or not by just calling text() and checking the content of the returned QString.
kind of


if(var a != NULL && var b != NULL)
{

//it's ok

}
else
{

//error

}

I personally deactivated the OK-Button if the input wasn't ok...

marcvanriet
18th November 2010, 11:54
Hi,

You can just check the 'ok' variable after each conversion. If it is false after any conversion, the user didn't fill in that field.

Regards,
Marc

NewLegend
18th November 2010, 12:35
Methods are not useful for reason simple :
How the user enter the data again ?

Have you heard of the try-catch ?

MarekR22
18th November 2010, 12:53
But Qt doesn't throw ANY exceptions.
You should create slot like that:
1. use QLineEdit::setValidator with QIntValidator

2. connect each signal editingFinished() to your slot where you will process data
3. close message box when all conversions are successful.

NewLegend
18th November 2010, 19:53
But Qt doesn't throw ANY exceptions.
You should create slot like that:
1. use QLineEdit::setValidator with QIntValidator

2. connect each signal editingFinished() to your slot where you will process data
3. close message box when all conversions are successful.

Are you sure that the exceptions "try-catch" is not in the Qt ??!!
:(

Timoteo
18th November 2010, 20:09
Yes.

http://tinyurl.com/28kvlcv

and even if Qt did throw, your code is using catch() for flow control with no handling whatsoever...not even a rethrow.

marcvanriet
18th November 2010, 20:31
Methods are not useful for reason simple :
How the user enter the data again ?

Simple... you probably have a form with an 'OK' or 'Submit' button that does some action with the data that is entered.

You do the checks first when the button is pressed in an on_xxx_clicked() slot. If not all data is entered you display the message box and just return. If all data is OK, you do your processing and maybe close the form in which data is entered.

Regards,
Marc

ChrisW67
19th November 2010, 02:40
Are you sure that the exceptions "try-catch" is not in the Qt ??!!

try and catch are C++ constructs and nothing to do with Qt. Of course you can use them when programming using Qt. However, they are only useful if things inside the try block actually throw exceptions. As MarekR22 and the Qt documentation points out, Qt does not throw exceptions during the normal course of events and certainly not to indicate a conversion failure when there's a bool returned specifically to indicate that.

You have several workable alternatives that others have pointed out already: stop invalid inputs in the first place, or trap invalid inputs after they have been made. If you are expecting ints from the UI then consider a QSpinBox instead of QLineEdit and save yourself some effort.

produktdotestow
19th November 2010, 14:18
why don't you type it like

if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty() || ui->lineEdit_2->text().isEmpty()){
QMessageBox::critical(NULL, QString("Error"),QString("You must enter all the variables id, Name and M_Case "));
} else{
// do what you want
}
?

or better (when you want to be sure that the variables are numeric) - make your isNumeric() method (or find some made) and replace isEmpty() with !isNumeric()