Auto close QMessageBox

前端 未结 5 2166
猫巷女王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: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);
        }
    }
    

提交回复
热议问题