How do I make a non-blocking, non-modal dialog equivalent to QMessageBox::information?
QMessageBox is a commonly used modal dialog to display some informational message and optionally ask the user to respond by clicking any one of the standard buttons on it. Each standard button has a predefined caption, a role and returns a predefined hexadecimal number. Sr.No.
A dialog is created with QMessageBox (). Don’t forget to import this from PyQt5. Then use the methods setIcon (), setText (), setWindowTitle () to set the window decoration.
The QMessageBox class provides a modal dialog for informing the user or for asking the user a question and receiving an answer. More... StandardButton { Ok, Open, Save, Cancel, Close, …, ButtonMask }
While you can construct these dialogs yourself, Qt also provides a built-in message dialog class called QMessageBox. This can be used to create information, warning, about or question dialogs. The example below creates a simple QMessageBox and shows it.
What do you mean by "unblocking"? Non-modal? Or one that doesn't block the execution until the user clicks ok? In both cases you'll need to create a QMessageBox manually instead of using the convenient static methods like QMessageBox::critical() etc.
In both cases, your friends are QDialog::open()
and 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)
}
Of course you can wrap that in your own helper functions so you don't have to duplicate it all over your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With