Important: Please read the Qt Code of Conduct -

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,
    msgBox->setInformativeText("-Wait ");

    my qmessagebox
    how to After 3 seconds Shutdown

  • Moderators

    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 of msgBox->exec() and start a single-shot timer for 3 seconds, that has its timeout() 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 to hide() or deleteLater(), 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,
    msgBox.setInformativeText("-Wait ");

    QTimer timer; // Don't use QTimer::singleShot()
    connect(&timer, &QTimer::timeout, [&]{ msgBox.close(); }); // with c++11


  • @Devopia53

    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()>)'

  • @ForestPoem

    If your compiler does not support C++11 lambda expressions, you can use following:

    connect(&timer, &QTimer::timeout, &msgBox, &QMessageBox::close);

Log in to reply