栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Qt使用定时器QTimer

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt使用定时器QTimer

目录

1. 头文件

2. cpp文件


1. 头文件

声明一个QTimer *ptimer指针;

声明一个普通类函数 void func();

声明一个槽函数(与普通的函数一致,但可以与多个信号连接)void test();

#ifndef TIMER_H
#define TIMER_H
#include 
#include 

namespace Ui {
class timer;
}

class timer : public QMainWindow
{
    Q_OBJECT

public:
    explicit timer(QWidget *parent = nullptr);
    QTimer *ptimer;
    void func();
    ~timer();
public slots:
    void test();
private:
    Ui::timer *ui;
};

#endif // TIMER_H

2. cpp文件

test()槽函数用于输出一个测试字符串

在func()中,首先初始化ptimer;

随后启动ptimer,start(10)表示10ms刷新一次定时器;

connect()表示将定时器连接test()函数,每次刷新定时器即刷新执行一次test()函数;

#include "timer.h"
#include "ui_timer.h"
#include 
timer::timer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::timer)
{
    ui->setupUi(this);
    func();
}
void timer::test(){
    qDebug() << "hello timer";
}

void timer::func(){
    ptimer = new QTimer(this);
    ptimer->start(10);
    connect(ptimer, SIGNAL(timeout()), this, SLOT(test()));

}

timer::~timer()
{
    delete ui;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号