我该如何创建一个与QMessageBox::information等效的非阻塞,非模态对话框?
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)
}
当然,你可以将其包装在自己的帮助函数中,这样你就不必在代码中重复了。