Results 1 to 11 of 11

Thread: MessageBox Validation

  1. #1
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default MessageBox Validation

    All,

    I have a QTextEdit box that is filled with text. I also have a button which clears the text when clicked.

    How would I make it so a message box appears when the button is clicked that would display "Are you sure you want to clear the log?" With a OK or Cancel button under it?


    Qt Code:
    1. void Autotester::clearlogs()
    2. {
    3. uip.AutoLog->clear();
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 


    Heres the connect for the button, the button that clears the text is named uip.autoClear.

    Qt Code:
    1. connect( uip.autoClear, SIGNAL ( clicked() ), this, SLOT( clearlogs() )); //clears Autotester log when clicked
    To copy to clipboard, switch view to plain text mode 

    Thanks guys

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    Qt Code:
    1. void Autotester::clearlogs()
    2. {
    3. QMessageBox::StandardButton answer = QMessageBox::question(this, tr("Clear the textbox"), tr("Are you sure?"), QMessageBox::Yes, QMessageBox::Cancel);
    4. if(answer == QMessageBox::Yes)
    5. uip.AutoLog->clear();
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to victor.fernandez for this useful post:

    fortyhideout12 (2nd September 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MessageBox Validation

    Thanks Victor.

    Heres my final code that works --
    Qt Code:
    1. void Autotester::uipAutoClearlogs()
    2. {
    3. int answer = QMessageBox::question(this, tr("Autotester Log"), tr("Are you sure you want to clear the current Autotester log?"), QMessageBox::Yes, QMessageBox::Cancel);
    4. if (answer == QMessageBox::Yes)
    5. uip.AutoLog->clear();
    To copy to clipboard, switch view to plain text mode 


    Now, my next question is. I want to put a validation in there to check if a user clicks on the clear button when there is no text available?

    I'd want a QMessageBox to appear that would state "There is no text to clear".

    Would I use another if on top of the statement?

  5. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    Would I use another if on top of the statement?
    Yes. To display the message, you may use QMessageBox::information().

  6. #5
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MessageBox Validation

    Qt Code:
    1. void tester::uipEulpClearlogs()
    2. {
    3. if (uip.ECULPLog.isEmpty())
    4. {
    5. QMessageBox::question(this, tr("BLANK LOG"), tr("It's already cleared dumbo"),QMessageBox::Yes);
    6. break;
    7. }
    8. else
    9.  
    10. int answer = QMessageBox::question(this, tr("ECULP Log"), tr("Are you sure you want to clear the current ECULP log?"), QMessageBox::Yes, QMessageBox::Cancel);
    11. if (answer == QMessageBox::Yes)
    12. uip.ECULPLog->clear();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    I'm getting the error
    -


    1>.\tester.cpp(1431) : error C2228: left of '.isEmpty' must have class/struct/union
    1> type is 'QTextEdit *'
    1> did you intend to use '->' instead?

  7. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    As the compiler suggests, you have to use -> because uip.ECULPLog is a pointer to an object, not an object itself.

    Qt Code:
    1. if (uip.ECULPLog->isEmpty())
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MessageBox Validation

    I tried that before and got this error.

    1>.\tester.cpp(1431) : error C2039: 'isEmpty' : is not a member of 'QTextEdit'
    1> c:\qt\4.5.1\include\qtgui\../../src/gui/widgets/qtextedit.h(70) : see declaration of 'QTextEdit'
    1>.\tester.cpp(1434) : error C2043: illegal break
    1>.\tester.cpp(1439) : error C2065: 'answer' : undeclared identifier

  9. #8
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    You also forgot the brackets:

    Qt Code:
    1. void tester::uipEulpClearlogs()
    2. {
    3. if (uip.ECULPLog->isEmpty())
    4. {
    5. QMessageBox::question(this, tr("BLANK LOG"), tr("It's already cleared dumbo"),QMessageBox::Yes);
    6. break;
    7. }
    8. else {
    9. int answer = QMessageBox::question(this, tr("ECULP Log"), tr("Are you sure you want to clear the current ECULP log?"), QMessageBox::Yes, QMessageBox::Cancel);
    10. if (answer == QMessageBox::Yes)
    11. uip.ECULPLog->clear();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    I tried that before and got this error.
    Oh, that's true. If it's a QLineEdit you need to call the text() method:

    Qt Code:
    1. if (uip.ECULPLog->text().isEmpty())
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to victor.fernandez for this useful post:

    fortyhideout12 (2nd September 2009)

  12. #10
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MessageBox Validation

    It's a QTextEdit.

    Qt Code:
    1. 1>.\tester.cpp(1431) : error C2039: 'text' : is not a member of 'QTextEdit'
    2. 1> c:\qt\4.5.1\include\qtgui\../../src/gui/widgets/qtextedit.h(70) : see declaration of 'QTextEdit'
    3. 1>.\tester.cpp(1431) : error C2228: left of '.isEmpty' must have class/struct/union
    4. 1>.\tester.cpp(1434) : error C2043: illegal break
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MessageBox Validation

    Then it's either toPlainText() or toHtml() instead of text(), depending on whether you're using rich text or plain text.

  14. The following user says thank you to victor.fernandez for this useful post:

    fortyhideout12 (2nd September 2009)

Similar Threads

  1. Ip Address Validation
    By bruccutler in forum Qt Programming
    Replies: 26
    Last Post: 3rd November 2010, 03:58
  2. Replies: 1
    Last Post: 12th November 2008, 08:15
  3. Suggestions for validation XML with XSD
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2008, 19:00
  4. Replies: 1
    Last Post: 21st April 2008, 22:43
  5. QSA 1.2.1 && MessageBox
    By xk in forum Newbie
    Replies: 0
    Last Post: 20th April 2006, 17:30

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.