QT学习_QTimer使用
Posted Leslie X徐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT学习_QTimer使用相关的知识,希望对你有一定的参考价值。
QT例子
日期和定时器
日期类:
- QTime
- QDate
- QDateTime
时间组件:
- QTimeEdit
- QDateEdit
- QDateTimeEdit
- QCalendarWidget
定时器类
- QTimer
属性:setInterval()
信号:timeout()
获取当前日期时间按钮:
void Widget::on_btn_GetTime_clicked()
{
//get current time
QDateTime curDateTime = QDateTime::currentDateTime();
ui->timeEdit->setTime(curDateTime.time());
ui->dateEdit->setDate(curDateTime.date());
ui->dateTimeEdit->setDateTime(curDateTime);
ui->editTime->setText(curDateTime.toString("hh:mm:ss"));
ui->editDate->setText(curDateTime.toString("yyyy-MM-dd"));
ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
string转时间:
void Widget::on_btnSetDateTime_clicked()
{
//string->QDataTime
QString str = ui->editDateTime->text();
str = str.trimmed();
if(!str.isEmpty())
{
QDateTime datetime = QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss");
ui->dateTimeEdit->setDateTime(datetime);
}
}
日历组件选择日期:
void Widget::on_calendarWidget_selectionChanged()
{
QDate dt = ui->calendarWidget->selectedDate();
QString str = dt.toString("yyyy/M/d");
ui->editCalendar->setText(str);
}
定时器设置:
//构造函数中初始化
fTimer = new QTimer(this);
fTimer->stop();
fTimer->setInterval(1000);
connect(fTimer,SIGNAL(timeout()),
this,SLOT(on_timer_timeout()));
//设置信号函数
void Widget::on_timer_timeout()
{
QTime curTime = QTime::currentTime();
ui->lcdhour->display(curTime.hour());
ui->lcdmin->display(curTime.minute());
ui->lcdsec->display(curTime.second());
int va = ui->progressBar->value();
va++;
if(va>100)va=0;
ui->progressBar->setValue(va);
}
//按键设置时间间隔
void Widget::on_btnSetPeriod_clicked()
{
int period = ui->spinPeriod->value();
fTimer->setInterval(period);
}
//设置开始和结束按键
void Widget::on_btnStart_clicked()
{
fTimer->start();
fTimeCounter.start();
ui->btnStart->setEnabled(false);
ui->btnStop->setEnabled(true);
ui->btnSetPeriod->setEnabled(false);
}
void Widget::on_btnStop_clicked()
{
fTimer->stop();
int tmMsec = fTimeCounter.elapsed();
int ms = tmMsec%1000;
int sec = tmMsec/1000;
QString str;
str.sprintf("%ds %dms",sec,ms);
ui->LabElapsTime->setText(str);
ui->btnStart->setEnabled(true);
ui->btnStop->setEnabled(false);
ui->btnSetPeriod->setEnabled(true);
}
结束按键:
void Widget::on_btnExit_clicked()
{
QApplication* app;
app->exit();
}
总体code
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
fTimer = new QTimer(this);
fTimer->stop();
fTimer->setInterval(1000);
connect(fTimer,SIGNAL(timeout()),
this,SLOT(on_timer_timeout()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn_GetTime_clicked()
{
//get current time
QDateTime curDateTime = QDateTime::currentDateTime();
ui->timeEdit->setTime(curDateTime.time());
ui->dateEdit->setDate(curDateTime.date());
ui->dateTimeEdit->setDateTime(curDateTime);
ui->editTime->setText(curDateTime.toString("hh:mm:ss"));
ui->editDate->setText(curDateTime.toString("yyyy-MM-dd"));
ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
void Widget::on_btnSetDateTime_clicked()
{
//string->QDataTime
QString str = ui->editDateTime->text();
str = str.trimmed();
if(!str.isEmpty())
{
QDateTime datetime = QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss");
ui->dateTimeEdit->setDateTime(datetime);
}
}
void Widget::on_btnSetTime_clicked()
{
QString str = ui->editTime->text();
str = str.trimmed();
if(!str.isEmpty())
{
QTime time = QTime::fromString(str,"hh:mm:ss");
ui->timeEdit->setTime(time);
}
}
void Widget::on_calendarWidget_selectionChanged()
{
QDate dt = ui->calendarWidget->selectedDate();
QString str = dt.toString("yyyy/M/d");
ui->editCalendar->setText(str);
}
void Widget::on_timer_timeout()
{
QTime curTime = QTime::currentTime();
ui->lcdhour->display(curTime.hour());
ui->lcdmin->display(curTime.minute());
ui->lcdsec->display(curTime.second());
int va = ui->progressBar->value();
va++;
if(va>100)va=0;
ui->progressBar->setValue(va);
}
void Widget::on_btnSetPeriod_clicked()
{
int period = 1000*ui->spinPeriod->value();
fTimer->setInterval(period);
}
void Widget::on_btnStart_clicked()
{
fTimer->start();
fTimeCounter.start();
ui->btnStart->setEnabled(false);
ui->btnStop->setEnabled(true);
ui->btnSetPeriod->setEnabled(false);
}
void Widget::on_btnStop_clicked()
{
fTimer->stop();
int tmMsec = fTimeCounter.elapsed();
int ms = tmMsec%1000;
int sec = tmMsec/1000;
QString str;
str.sprintf("%ds %dms",sec,ms);
ui->LabElapsTime->setText(str);
ui->btnStart->setEnabled(true);
ui->btnStop->setEnabled(false);
ui->btnSetPeriod->setEnabled(true);
}
void Widget::on_btnExit_clicked()
{
QApplication* app;
app->exit();
}
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include <QTime>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_btn_GetTime_clicked();
void on_btnSetDateTime_clicked();
void on_btnSetTime_clicked();
void on_calendarWidget_selectionChanged();
void on_timer_timeout();
void on_btnSetPeriod_clicked();
void on_btnStart_clicked();
void on_btnStop_clicked();
void on_btnExit_clicked();
private:
Ui::Widget *ui;
QTimer *fTimer;
QTime fTimeCounter;
};
#endif // WIDGET_H
以上是关于QT学习_QTimer使用的主要内容,如果未能解决你的问题,请参考以下文章