PDA

View Full Version : QMessageBox inside closeEvent



zgulser
9th November 2012, 12:31
Hi,

I have the following code;



void GroupChatViewController::closeEvent(QCloseEvent* event)
{
QMessageBox* msgBox = new QMessageBox(0);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setWindowTitle(tr("Quit Group Chat?"));
msgBox->setText(tr("Are your sure to quit from group chat?"));
msgBox->setIcon(QMessageBox::Question);
QPushButton* yesButton = msgBox->addButton(tr("Yes"), QMessageBox::AcceptRole);
QPushButton* noButton = msgBox->addButton(tr("Cancel"), QMessageBox::RejectRole);
msgBox->setDefaultButton(noButton);
msgBox->exec();

if(msgBox->clickedButton() == noButton)
{
msgBox->close();
return;
}
else if(msgBox->clickedButton() == yesButton)
{
if(_groupImSession)
{
if(_groupImSession->_groupImSessionEstablished)
{
_groupImSessionManager->closeGroupImSession(_groupImSession);
}
}

QWidget::closeEvent(event);
}
}


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

Santosh Reddy
9th November 2012, 12:38
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.

zgulser
9th November 2012, 12:54
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.