QMessagebox I would like to know how the three seconds shut down.
-
QMessageBox* msgBox = new QMessageBox(QMessageBox::Information,
"This is the title",
"OK Button Result",
QMessageBox::Ok, this,
Qt::FramelessWindowHint);
msgBox->setInformativeText("-Wait ");
msgBox->exec();my qmessagebox
how to After 3 seconds Shutdown -
You can't, not with this code anyway. You could create a modeless dialog (QMessageBox is a QDialog subclass), meaning you call
msgBox-show()
instead ofmsgBox->exec()
and start a single-shot timer for 3 seconds, that has itstimeout()
signal connected to the slot you want to use for shutdown (i.e. if shutdown means just hiding the message box, you either connect it tohide()
ordeleteLater()
, depending on your particular needs). Bear in mind that while the modeless message box will not block the program, you might need to deal with the case of losing focus - the user clicking on your main window/widget etc. -
You can use the following: use the modal dialog...
QMessageBox msgBox(QMessageBox::Information,
"This is the title",
"OK Button Result",
QMessageBox::Ok, this,
Qt::FramelessWindowHint);
msgBox.setInformativeText("-Wait ");QTimer timer; // Don't use QTimer::singleShot()
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, [&]{ msgBox.close(); }); // with c++11
timer.start(3000);msgBox.exec();
-
You can use the following: use the modal dialog...
QMessageBox msgBox(QMessageBox::Information,
"This is the title",
"OK Button Result",
QMessageBox::Ok, this,
Qt::FramelessWindowHint);
msgBox.setInformativeText("-Wait ");QTimer timer; // Don't use QTimer::singleShot()
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, [&]{ msgBox.close(); }); // with c++11
timer.start(3000);msgBox.exec();
source code dump is error
error: 'void QTimer::timeout()' is protected
error: within this context
error: no matching function for call to 'class::connect(QTimer*, void (QTimer::)(), class::class(QWidget)::<lambda()>)'
-
source code dump is error
error: 'void QTimer::timeout()' is protected
error: within this context
error: no matching function for call to 'class::connect(QTimer*, void (QTimer::)(), class::class(QWidget)::<lambda()>)'
If your compiler does not support C++11 lambda expressions, you can use following:
connect(&timer, &QTimer::timeout, &msgBox, &QMessageBox::close);