Results 1 to 3 of 3

Thread: QMessageBox inside closeEvent

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default QMessageBox inside closeEvent

    Hi,

    I have the following code;

    Qt Code:
    1. void GroupChatViewController::closeEvent(QCloseEvent* event)
    2. {
    3. QMessageBox* msgBox = new QMessageBox(0);
    4. msgBox->setAttribute(Qt::WA_DeleteOnClose);
    5. msgBox->setWindowTitle(tr("Quit Group Chat?"));
    6. msgBox->setText(tr("Are your sure to quit from group chat?"));
    7. msgBox->setIcon(QMessageBox::Question);
    8. QPushButton* yesButton = msgBox->addButton(tr("Yes"), QMessageBox::AcceptRole);
    9. QPushButton* noButton = msgBox->addButton(tr("Cancel"), QMessageBox::RejectRole);
    10. msgBox->setDefaultButton(noButton);
    11. msgBox->exec();
    12.  
    13. if(msgBox->clickedButton() == noButton)
    14. {
    15. msgBox->close();
    16. return;
    17. }
    18. else if(msgBox->clickedButton() == yesButton)
    19. {
    20. if(_groupImSession)
    21. {
    22. if(_groupImSession->_groupImSessionEstablished)
    23. {
    24. _groupImSessionManager->closeGroupImSession(_groupImSession);
    25. }
    26. }
    27.  
    28. QWidget::closeEvent(event);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    The message diaolog appears but the problem here is program doesn't advance neither when I click noButton nor yesButton.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QMessageBox inside closeEvent

    You have set delete on close, so when you get the control out of exec(), the message box may have already been deleted / marked for deletion. operating on it after exec() is bad idea.

    instead don't set the delete on close flag, and use the message box after exec(), and make sure to explicitly delete it before you return the call. Another better approach is to create message box on stack, this way it be deleted anyway.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    zgulser (9th November 2012)

  4. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: QMessageBox inside closeEvent

    Oops.. I thought that they were deleted somewhere like the destuctor of QMessgaBox. But they may be marked as well as you mentioned.

    Thanks for the idea.

Similar Threads

  1. Replies: 8
    Last Post: 21st December 2011, 17:11
  2. CloseEvent of QDockWidget
    By mstegehu in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2010, 13:10
  3. QDialog and closeEvent
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2009, 02:07
  4. QSettings and closeEvent()
    By vito49 in forum Newbie
    Replies: 2
    Last Post: 13th October 2008, 16:18
  5. closeEvent
    By jochen_r in forum Newbie
    Replies: 7
    Last Post: 16th January 2006, 12: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.