日期
定时器
需要引入的头文件 #include
QTimer:定时器,用来处理周期性事件的一种对象,类似于硬件定时器,例如设置一个定时器的定时周期为1000毫秒,那么每1000毫秒就会发射定时器的timeout()信号,在信号关联的槽函数里就可以做相应的处理。
日期显示格式符
图片示例
代码示例
DateTimeDialog.h
#ifndef DATETIMEDIALOG_H
#define DATETIMEDIALOG_H
#include
#include
namespace Ui
{
class DateTimeDialog;
}
class DateTimeDialog : public QDialog
{
Q_OBJECT
public:
explicit DateTimeDialog(QWidget* parent = nullptr);
~DateTimeDialog();
public slots:
void on_pushButton_clicked();
void on_pushButtonSetTime_clicked();
void on_pushButtonSetDate_clicked();
void on_pushButtonSetDateTime_clicked();
void on_calendarWidget_selectionChanged();
void timeOutMethod();
private slots:
void on_pushButtonStart_clicked();
void on_pushButtonStop_clicked();
private:
Ui::DateTimeDialog* ui;
QTimer* timer;
};
#endif // DATETIMEDIALOG_H
DateTimeDialog.cpp
#include "DateTimeDialog.h"
#include "ui_DateTimeDialog.h"
DateTimeDialog::DateTimeDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::DateTimeDialog)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timeOutMethod()));
ui->pushButtonStop->setEnabled(false);
ui->spinBox->setSuffix("ms");
ui->spinBox->setMaximum(10000000);
ui->spinBox->setMinimum(100);
}
DateTimeDialog::~DateTimeDialog()
{
delete ui;
}
void DateTimeDialog::on_pushButton_clicked()
{
//获取当前时间
QDateTime curDateTime = QDateTime::currentDateTime();
ui->timeEdit->setTime(curDateTime.time());
ui->editTime->setText(curDateTime.toString("hh:mm:ss"));
ui->dateEdit->setDate(curDateTime.date());
ui->editDate->setText(curDateTime.toString("yyyy-MM-dd"));
ui->dateTimeEdit->setDateTime(curDateTime);
ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
void DateTimeDialog::on_pushButtonSetTime_clicked()
{
QString str = ui->editTime->text();
str = str.trimmed();
QDateTime dateTime = QDateTime::fromString(str, "hh:mm:ss");
ui->timeEdit->setTime(dateTime.time());
}
void DateTimeDialog::on_pushButtonSetDate_clicked()
{
QString str = ui->editDate->text();
str = str.trimmed();
QDateTime dateTime = QDateTime::fromString(str, "yyyy-MM-dd");
ui->dateEdit->setDate(dateTime.date());
}
void DateTimeDialog::on_pushButtonSetDateTime_clicked()
{
QString str = ui->editDateTime->text();
str = str.trimmed();
QDateTime dateTime = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
ui->dateTimeEdit->setDateTime(dateTime);
}
void DateTimeDialog::on_calendarWidget_selectionChanged()
{
QDate dt = ui->calendarWidget->selectedDate();
QString str = dt.toString("yyyy-MM-dd");
ui->editCalendar->setText(str);
}
void DateTimeDialog::timeOutMethod()
{
QTime curTime = QTime::currentTime();
ui->lcdNumberH->display(curTime.hour());
ui->lcdNumberM->display(curTime.minute());
ui->lcdNumberS->display(curTime.msec());
}
void DateTimeDialog::on_pushButtonStart_clicked()
{
timer->setInterval(ui->spinBox->value());
timer->start();
ui->pushButtonStop->setEnabled(true);
ui->pushButtonStart->setEnabled(false);
}
void DateTimeDialog::on_pushButtonStop_clicked()
{
timer->stop();
ui->pushButtonStop->setEnabled(false);
ui->pushButtonStart->setEnabled(true);
}