如何制作非阻塞,非模态的QMessageBox?

gre*_*pta 9 qt

如何创建一个与QMessageBox :: information等效的非阻塞,非模态对话框?

Fra*_*eld 32

"解锁"是什么意思?非模态?或者在用户点击确定之前不阻止执行的那个?在这两种情况下,您都需要手动创建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)
}
Run Code Online (Sandbox Code Playgroud)

当然,您可以将它包装在您自己的帮助程序函数中,这样您就不必在代码中复制它.