1.首先看效果图
2.创建子进程,写时间
void timethread::run()
{
QString str_datetime,str_curtime;
while (1) {
QDateTime current_datetime = QDateTime::currentDateTime();
str_datetime = current_datetime.toString("hh : mm : ss");
emit senddatetime(str_datetime);
QTime current_time = QTime::currentTime();
int hour = current_time.hour();
int min = current_time.minute();
int sec = current_time.second();
hour = 18 - hour;
min = 60 - min;
sec = 60 - sec;
if(0-sec < 0)
{
min --;
}
if(0-min<0)
{
hour--;
}
str_curtime = QString("%1 : %2 : %3").arg(hour).arg(min).arg(sec);
emit sendneedtime(str_curtime);
msleep(1000);
}
}
3.在主线程引用
timethread *thread = new timethread;
thread->start();
connect(thread,&timethread::senddatetime,this,&MainWindow::cur_datetime);
connect(thread,&timethread::sendneedtime,this,&MainWindow::need_time);
4.定义槽函数
void MainWindow::cur_datetime(QString str_datetime)
{
ui->datetimeLabel->setText(str_datetime);
}
void MainWindow::need_time(QString str_curtime)
{
ui->label->setText(str_curtime);
}