码迷,mamicode.com
首页 > 其他好文 > 详细

Qt学习: QTimerEvent定时器事件的处理程序代码示例

时间:2017-10-12 22:53:41      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:软件   window   编程   

重要函数: 
1.int startTimer(int); //设置定时器,返回一个ld. 
2.int event->timerld(); //返回当前的ld. 
3.void killTimer(int); //停止定时器.


首先从Qt设计师中拖拽出三个按钮,由于只是演示定时器事件的使用,所以就没有布局的需要了. 
技术分享


以下是”c.cpp”的代码:

#include "c.h"c::c(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);    //连接信号与槽.
    connect(ui.startButton, SIGNAL(clicked()), this, SLOT(startTimerSlot()));
    connect(ui.stopButton, SIGNAL(clicked()), this, SLOT(stopTimerSlot()));
}

c::~c()
{

}void c::timerEvent(QTimerEvent*event)
{    //判断当前定时器对应的是哪个ld.
    if (event->timerId() == this->m_lamp)
    {        if (this->m_lampStatus == false)
        {            //设置按钮的图标.
            ui.toolButton->setIcon(QIcon("Icons/lamp.png"));            this->m_lampStatus = true;
        }        else
        {
            ui.toolButton->setIcon(QIcon("Icons/space.png")); 
            this->m_lampStatus = false;
        }
    }
}void c::startTimerSlot()
{    //设置定时器,返回一个timerld.注意单位为毫秒,1000毫秒等于1秒.
    this->m_lamp = this->startTimer(1000);
}void c::stopTimerSlot()
{    //停止定时器.
    this->killTimer(this->m_lamp);
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546

以下是”c.h”的代码:

#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QLabel>#include <QTimerEvent>class c : public QMainWindow
{
    Q_OBJECTpublic:    c(QWidget *parent = 0);
    ~c();protected:
    //这是一个虚函数,从QEvent继承而来.    void timerEvent(QTimerEvent*event);private slots:    void startTimerSlot();    void stopTimerSlot();private:
    Ui::cClass ui;    int m_lamp;    bool m_lampStatus = false;
};#endif // C_H12345678910111213141516171819202122232425262728293031

最后是”main.cpp”的代码:

#include "c.h"#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    c w;
    w.show();    return a.exec();
}


本文出自 “13109855” 博客,请务必保留此出处http://13119855.blog.51cto.com/13109855/1971837

Qt学习: QTimerEvent定时器事件的处理程序代码示例

标签:软件   window   编程   

原文地址:http://13119855.blog.51cto.com/13109855/1971837

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!