QT 按钮类继承处理带定时器

01.class KeyButton : public QPushButton 
02.{ 
03.    Q_OBJECT 
04.public: 
05.    explicit KeyButton(QWidget *parent = 0) : QPushButton(parent), 
06.        pauseMsecs(400), intervalMsecs(30) 
07.    { 
08.        tm = new QTimer(this); 
09.        connect(tm, SIGNAL(timeout()), this, SLOT(on_pressed_last())); 
10.        connect(this, SIGNAL(pressed()), this, SLOT(on_pressed())); 
11.        connect(this, SIGNAL(released()), this, SLOT(on_released())); 
12.        connect(this, SIGNAL(clicked()), this, SLOT(on_clicked())); 
13.    } 
14. 
15.private: 
16.    QTimer *tm; 
17.    long pauseMsecs; 
18.    long intervalMsecs; 
19. 
20.signals: 
21.    void keyPressed(const QString &msg); 
22.    void keyReleased(const QString &msg); 
23.    void keyClicked(const QString &msg); 
24. 
25.public slots: 
26.    void on_pressed() { emit this->keyPressed(this->text()); 
27.                        tm->start(pauseMsecs); } 
28.    void on_pressed_last() { emit this->keyPressed(this->text()); 
29.                             tm->setInterval(intervalMsecs); } 
30.    void on_released() { tm->stop(); emit this->keyReleased(this->text()); } 
31.    void on_clicked() { emit this->keyClicked(this->text()); } 
32.};

posted @ 2015-04-01 16:52  luxiaolai  阅读(341)  评论(0编辑  收藏  举报