时间日期与定时器
时间日期相关的类
(https://blog.csdn.net/theRookie1/article/details/84672489)
时间日期数据与字符串之间转换
void Widget::on_pushButton_clicked()//获取当前时间日期按钮
{
QDateTime curDateTime=QDateTime::currentDateTime();//通过QDateTime的currentDateTime获得当前的日期时间,并赋值给curDateTime
ui->timeEdit->setTime(curDateTime.time());//为时间编辑器设置当前的时间
ui->editTime->setText(curDateTime.time().toString("hh:mm"));//将获得的时间转化为字符串
ui->dateEdit->setDate(curDateTime.date());//为日期编辑器设置当前的日期
//ui->editDate->setText(curDateTime.date().toString("yyyy年MM月dd日"));//将获得的日期转化为字符串
ui->editDate->setText(curDateTime.date().toString("yyyy-MM-dd"));//将获得的日期转化为字符串可以使用汉字如年,月,日
ui->dateTimeEdit->setDateTime(curDateTime);//为日期时间编辑器设置当前的日期时间
ui->editDateTime->setText(curDateTime.toString("yyyy-MM-dd hh:mm"));//将获得的日期时间转换为字符串
//日期显示的格式符及其意义
//d 天,不补零显示,1-31
//dd 天,补零显示,01-31
//M 月,不补零显示,1-12
//MM 月,补零显示,01-12
//yy 年,两位显示,00-99
//yyyy 年,四位显示,0000-9999
//h 小时,不补零,0-23或1-12(如果显示AM/PM)
//hh 小时,补零,00-23或01-12(如果显示AM/PM)
//H 小时,不补零,0-23(即使显示AM/PM)
//HH 小时,补零,00-23(即使显示AM/PM)
//m 分钟,不补零,0-59
//mm 分钟,补零,00-59
//z 毫秒,不补零,0-999
//zzz 毫秒,补零,000-999
//AP或A 使用AM/PM显示
//ap或a 使用am/pm显示
}
void Widget::on_pushButton_2_clicked()//设置时间
{
QString str=ui->editTime->text();
str=str.trimmed();//去除首尾的空格
if(!str.isEmpty())
{
QTime time=QTime::fromString(str,"hh:mm");//将字符串以一定的格式输出
ui->timeEdit->setTime(time);//在时间编辑器中显示时间
}
}
void Widget::on_pushButton_3_clicked()//设置日期
{
QString str=ui->editDate->text();
str=str.trimmed();
if(!str.isEmpty())
{
QDate date=QDate::fromString(str,"yyyy-MM-dd");//这里的格式应该和dateEdit格式相同
ui->dateEdit->setDate(date);
}
}
void Widget::on_pushButton_4_clicked()//设置日期时间
{
QString str=ui->editDateTime->text();
str=str.trimmed();
if(!str.isEmpty())
{
QDateTime datetime=QDateTime::fromString(str,"yyyy-MM-dd hh:mm");//这里的格式应该和dateTimeEdit格式相同
ui->dateTimeEdit->setDateTime(datetime);
}
}
QCalendarWidget日历组件的使用
其中也包含了日期数据与字符串的转换
void Widget::on_calendarWidget_selectionChanged()//QCalendarWidget的selectionChanged()信号
//在日历上选择的日期发生变化时会发射此信号
{
QDate date=ui->calendarWidget->selectedDate();
//QString str=date.toString("yyyy-MM-dd");
//ui->editCalendar->setText(str);
ui->editCalendar->setText(date.toString("yyyy-MM-dd"));
}
定时器的使用
private:
QTimer *fTimer;//定时器
QTime fTimeCount;//计时器
void on_timer_timeout();//定时溢出处理槽函数
fTimer=new QTimer(this);//实例化对象
fTimer->stop();
fTimer->setInterval(1000);//设置定时周期,单位:毫秒
connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));//关联fTimer的timeout()信号与槽函数on_timer_out()
void Widget::on_timer_timeout()
{
QTime curtime=QTime::currentTime();//获取当前时间
ui->lcdNumber->display(curtime.hour());//显示小时
ui->lcdNumber_2->display(curtime.minute());//显示分钟
ui->lcdNumber_3->display(curtime.second());//显示秒钟
int pro=ui->progressBar->value();
pro++;
if(pro>100)
pro=0;
ui->progressBar->setValue(pro);//progressBar的变化
}
void Widget::on_pushButton_7_clicked()//设置周期按钮
{
int interval=ui->spinBox->value();
fTimer->setInterval(interval);
ui->pushButton_5->setEnabled(true);//开始按钮有效
ui->pushButton_6->setEnabled(false);//停止按钮无效
ui->pushButton_7->setEnabled(false);//设置周期按钮无效
}
void Widget::on_pushButton_5_clicked()//开始按钮
{
fTimer->start();//定时器开工作
fTimeCount.start();//计数器开始工作
ui->pushButton_5->setEnabled(false);//开始按钮无效
ui->pushButton_6->setEnabled(true);//停止按钮有效
ui->pushButton_7->setEnabled(false);//设置周期按钮无效
}
void Widget::on_pushButton_6_clicked()//停止按钮
{
fTimer->stop();
int tmMsec=fTimeCount.elapsed();//计算时间差
int ms=tmMsec%1000;//计算毫秒数
int sec=tmMsec/1000;//计算秒数
QString str=QString::asprintf("流逝时间:%d秒,%d毫秒",sec,ms);
ui->label_6->setText(str);
ui->pushButton_5->setEnabled(true);//开始按钮有效
ui->pushButton_6->setEnabled(false);//停止按钮无效
ui->pushButton_7->setEnabled(true);//设置周期按钮有效
}
其中可以为设置周期的spinBox的value属性中设定初始值,例如我为了贴合程序我将value设为1000ms,也就是1s
运行结果