如何制作一个非阻塞、非模态的QMessageBox?

13
1个回答

35
你所说的“解除阻塞”是什么意思?是指非模态窗口吗?还是指不会阻塞执行,直到用户点击确定的窗口?在这两种情况下,你都需要手动创建QMessageBox,而不是使用方便的静态方法如QMessageBox::critical()等。
在这两种情况下,你可以使用QDialog::open()和QMessageBox::open(QObject*,const char*)。
void MyWidget::someMethod() {
   ...
   QMessageBox* msgBox = new QMessageBox( this );
   msgBox->setAttribute( Qt::WA_DeleteOnClose ); //makes sure the msgbox is deleted automatically when closed
   msgBox->setStandardButtons( QMessageBox::Ok );
   msgBox->setWindowTitle( tr("Error") );
   msgBox->setText( tr("Something happened!") );
   msgBox->setIcon...
   ...
   msgBox->setModal( false ); // if you want it non-modal
   msgBox->open( this, SLOT(msgBoxClosed(QAbstractButton*)) );

   //... do something else, without blocking
}

void MyWidget::msgBoxClosed(QAbstractButton*) {
   //react on button click (usually only needed when there > 1 buttons)
}

当然,你可以将其包装在自己的帮助函数中,这样你就不必在代码中重复了。


非常感谢,我正在寻找同样的东西。 - greshi Gupta
这并不提供一个真正的非模态对话框QMessageBox! - Mohammad Kanan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接