定时关闭消息对话框


class QTimeDialog : public QDialog
{
public:
    QTimeDialog (const QString & str,QWidget *parent = NULL);
    ~QTimeDialog ();

    static void show(const QString & msg, QWidget *parent = NULL);

protected:
    void startShow();
    virtual void timerEvent(QTimerEvent * event);

private:
    QLabel * m_display;
    QHBoxLayout * m_layout;
    QString m_msg;
    int m_timeCount;
    int m_timerId;
    static QTimeDialog  * _timeDialg_;
};



//
#define MAX_MSG_SHOW 60
//----------------------------------------------------------------------------------
QTimeDialog *::QTimeDialog _timeDialg_ = NULL;
//-----------------------------------------------------------------------------
QTimeDialog ::(QTimeDialog const QString & str, QWidget *parent)
    :m_msg(str)
    ,QDialog(parent)
    ,m_timeCount(0)
    ,m_timerId(0)
{
    m_display = new QLabel;
    m_layout = new QHBoxLayout;
 
   
    m_layout->addWidget(m_display);
 
   
    this->setLayout(m_layout);
}
 
   
QTimeDialog ::~QTimeDialog()
{
    this->killTimer(m_timerId);
    _timeDialg_ = NULL;
}
 
   
void QTimeDialog::show(const QString & msg,QWidget * parent)
{
    if(_timeDialg_)
    {
        _timeDialg_->m_timeCount = 0;
        _timeDialg_->m_display->setText(msg);
    }
    else
    {
        _timeDialg_ = new GZLTimeDialog(msg,parent);
        _timeDialg_->startShow();
    }
}
 
   
void QTimeDialog::timerEvent(QTimerEvent * event)
{
    m_timeCount++;
    if(m_timeCount > MAX_MSG_SHOW)
    {
        m_timeCount = 0;
        this->close();
    }
}
 
   
void QTimeDialog::startShow()
{
    QDialog::show();
    m_timerId = this->startTimer(1000);
}

猜你喜欢

转载自blog.csdn.net/wolfseek/article/details/41729783