Results 1 to 10 of 10

Thread: QMessageBox If not enter all the variables

  1. #1
    Join Date
    Jul 2010
    Posts
    71
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QMessageBox If not enter all the variables

    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.
    Qt Code:
    1. try
    2. {
    3. bool ok;
    4. int id = ui->lineEdit->text().toInt(&ok,10);
    5. int Name= ui->lineEdit_2->text().toInt(&ok,10);
    6. int M_Case = ui->lineEdit_3->text().toInt(&ok,10);
    7. }
    8. catch (std::exception& e) {
    9. QMessageBox::critical(NULL, QString("Error"),QString("You must enter all the variables id, Name and M_Case "));
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox If not enter all the variables

    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
    Qt Code:
    1. if(var a != NULL && var b != NULL)
    2. {
    3.  
    4. //it's ok
    5.  
    6. }
    7. else
    8. {
    9.  
    10. //error
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    I personally deactivated the OK-Button if the input wasn't ok...

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox If not enter all the variables

    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

  4. #4
    Join Date
    Jul 2010
    Posts
    71
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox If not enter all the variables

    Methods are not useful for reason simple :
    How the user enter the data again ?

    Have you heard of the try-catch ?

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QMessageBox If not enter all the variables

    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.

  6. #6
    Join Date
    Jul 2010
    Posts
    71
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox If not enter all the variables

    Quote Originally Posted by MarekR22 View Post
    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 ??!!

  7. #7
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox If not enter all the variables

    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.

  8. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox If not enter all the variables

    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

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMessageBox If not enter all the variables

    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.

  10. #10
    Join Date
    Feb 2010
    Location
    Poland
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox If not enter all the variables

    why don't you type it like
    Qt Code:
    1. if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty() || ui->lineEdit_2->text().isEmpty()){
    2. QMessageBox::critical(NULL, QString("Error"),QString("You must enter all the variables id, Name and M_Case "));
    3. } else{
    4. // do what you want
    5. }
    To copy to clipboard, switch view to plain text mode 
    ?

    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()
    Last edited by produktdotestow; 19th November 2010 at 14:25.

Similar Threads

  1. How to disable enter
    By phillip_Qt in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2012, 07:52
  2. How to use ENTER key on QListWidget
    By sanket.mehta in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2010, 13:05
  3. QTextBrowser and ENTER key
    By szarek in forum Qt Programming
    Replies: 5
    Last Post: 10th July 2010, 20:47
  4. Replies: 1
    Last Post: 14th September 2008, 23:05
  5. QEvent::Enter
    By incapacitant in forum Newbie
    Replies: 6
    Last Post: 22nd March 2006, 08:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.