Results 1 to 4 of 4

Thread: Handling MessgeBox in CloseEvent

  1. #1
    Join Date
    Aug 2014
    Posts
    28
    Thanks
    15
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation Handling MessgeBox in CloseEvent

    Hi,
    why when i press no button the program will close?

    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *event){
    2. QMessageBox msg(this);
    3. msg.setText("Are you sure you want to Quit?");
    4. QAbstractButton *no=msg.addButton(tr("No"),QMessageBox::ActionRole);
    5. QAbstractButton *yes=msg.addButton(tr("Yes"),QMessageBox::ActionRole);
    6. if(msg.clickedButton()==no){
    7. event->ignore();
    8. }
    9. else{
    10. event->accept();
    11. }
    12. msg.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    thank u

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Handling MessgeBox in CloseEvent

    The code you posted makes no sense. With the current code, regardless what you do with the message box, the window will close. Try this:

    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *event) {
    2. if(QMessageBox::question("Are you sure you want to quit?") != QMessageBox::Yes) event->ignore();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    ramin.lich (28th November 2014)

  4. #3
    Join Date
    Aug 2014
    Posts
    28
    Thanks
    15
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Handling MessgeBox in CloseEvent

    thanks but
    give me an error....

    no matching function for call to 'QMessageBox::question(const char [31])'
    ^

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Handling MessgeBox in CloseEvent

    So lookup the function in the docs and adjust your code to the required signature.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. CloseEvent error
    By hollowhead in forum Newbie
    Replies: 2
    Last Post: 10th March 2012, 12:21
  2. Can't override the closeEvent()
    By scot_hansen in forum Newbie
    Replies: 7
    Last Post: 5th October 2010, 23:47
  3. QSettings and closeEvent()
    By vito49 in forum Newbie
    Replies: 2
    Last Post: 13th October 2008, 17:18
  4. closeEvent help needed
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 18:51
  5. closeEvent
    By jochen_r in forum Newbie
    Replies: 7
    Last Post: 16th January 2006, 13:05

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.