网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

创新互联自成立以来,一直致力于为企业提供从网站策划、网站设计、网站设计制作、成都做网站、电子商务、网站推广、网站优化到为企业提供个性化软件开发等基于互联网的全面整合营销服务。公司拥有丰富的网站建设和互联网应用系统开发管理经验、成熟的应用系统解决方案、优秀的网站开发工程师团队及专业的网站设计师团队。


首先从Qt设计师中拖拽出三个按钮,由于只是演示定时器事件的使用,所以就没有布局的需要了. 
 Qt学习: QTimerEvent定时器事件的处理程序代码示例


以下是”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 #include "ui_c.h"#include #include 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 int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    c w;
    w.show();    return a.exec();
}

名称栏目:Qt学习:QTimerEvent定时器事件的处理程序代码示例
网页链接:http://bjjierui.cn/article/giddhe.html

其他资讯