Auto close QMessageBox

前端 未结 5 2124
猫巷女王i
猫巷女王i 2020-12-31 19:20

I\'m building a Qt Symbian Project and I want to show a notification for the user that should auto close after some seconds. I have seen that Nokia uses this a lot in their

相关标签:
5条回答
  • 2020-12-31 19:39

    Instead you can use Singleshot Timer to close any dialog box or QLabel with much ease:

    QTimer *timer;
    QTimer::singleShot(10000, msgBox, SLOT(close()));
    
    0 讨论(0)
  • 2020-12-31 19:45

    I would suggest to subclass QMessageBox to add your own desired behavior...

    It would be interesting to add methods like setAutoClose(bool) and setAutoCloseTimeout(int) and trigger a QTimer on showEvent when the AutoClose option is enabled !

    This way, you could even alter the apparence of your QMessageBox and had a text saying "This box will close automatically in XXX seconds..." or a progress bar, etc...

    0 讨论(0)
  • 2020-12-31 19:49

    Thanks really much! My solution:

    I created my own class (MessageBox) this is my code for showing it:

    MessageBox msgBox;
    msgBox.setText("Hello!");
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.setAutoClose(true);
    msgBox.setTimeout(3); //Closes after three seconds
    msgBox.exec();
    

    This is my class:

    class MessageBox : public QMessageBox
    
    int timeout;
    bool autoClose;
    int currentTime;
    
    void MessageBox::showEvent ( QShowEvent * event ) {
        currentTime = 0;
        if (autoClose) {
        this->startTimer(1000);
        }
    }
    
    void MessageBox::timerEvent(QTimerEvent *event)
    {
        currentTime++;
        if (currentTime>=timeout) {
        this->done(0);
        }
    }
    
    0 讨论(0)
  • 2020-12-31 19:52

    With this code:

    QTimer *timer;
    QTimer::singleShot(10000, msgBox, SLOT(close()));
    

    you get:

    QObject::connect: Incompatible sender/receiver arguments
            QTimer::timeout() --> QMessageBox::
    

    Becouse msgBOx (the receiver) must be a QtCore object.. and QMessageBox subclassing QtGui. See https://srinikom.github.io/pyside-docs/PySide/QtCore/QTimer.html#PySide.QtCore.PySide.QtCore.QTimer.singleShot

    0 讨论(0)
  • 2020-12-31 19:56

    This may help someone,

    msgBox.button(QMessageBox::Ok)->animateClick(5000);
    

    The messageBox closes after 5 seconds.

    0 讨论(0)
提交回复
热议问题