项目里需要用到一个功能,定时给服务器发送消息,这里可以采用qt的QTimer类来实现
原理介绍啥的就不多说了,直接上代码啦
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));//要自定义update函数实现自己的功能哟
timer->start(3000);//设置时间间隔为3000毫秒
}
void MainWindow::update()
{
int i=QDateTime::currentDateTime().toTime_t();
qDebug()<<QDateTime::fromTime_t(i).toString("yyyy-MM-dd hh:mm:ss.zzz");
}
MainWindow::~MainWindow()
{
delete ui;
}
程序运行结果如下:
可以看到确实是3秒就update一下哦~
emmm,大家如果可以关注下我的个人博客啦~