qt中如何取得系统时间并让它动态的显示到label标签上?

qt中如何取得系统时间并让它动态的显示到label标签上?,第1张

¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥实现方法如下: 构造函数中加入: QTimer *timer = new QTimer(this)connect(timer,SINGAL(timeout()),this,SLOT(timer_deal_slot_function()))槽函数这样写:(获取系统时间) QDateTime dateTime = QDateTime::currentDateTime()int y=dateTime.date().year()int m=dateTime.date().month()int d=dateTime.date().day()QString strTime=dateTime.time().toString()ui->label->setText(strTime+" "+QString::number(y)+"/"+QString::number(m)+"/"+QString::number(d))//显示时间格式为例如 12:24:33 2008/8/28 ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥

第一步,创建一个dateTimeEdit,第二步,将其connect到一个函数上,第三部,提取它的QDateTime,然后利用下面的函数就可以了,它是基于shell命令的:

void moreidDialog::slot_save_time_key()

{

QDateTime time

QString str = txt_time->text()

//判断格式是否正确

if (str.count(".") != 4)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

int i = 0,j = 0

i = str.indexOf(".")

QString year = str.mid(0,i)

j = str.indexOf(".",i + 1)

QString month = str.mid(i + 1,j - i - 1)

i = j

j = str.indexOf(".",i + 1)

QString day = str.mid(i + 1,j - i - 1)

i = j

j = str.indexOf(".",i + 1)

QString hour = str.mid(i + 1,j - i - 1)

i = j

j = str.indexOf(".",i + 1)

QString min = str.mid(i + 1,j - i - 1)

bool ok = false

year.toInt(&ok)

if (ok == false)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

month.toInt(&ok)

if (ok == false)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

day.toInt(&ok)

if (ok == false)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

hour.toInt(&ok)

if (ok == false)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

min.toInt(&ok)

if (ok == false)

{

txt_time->setText(tr("ge shi cuo wu"))

return

}

str = "date -s " + month + "/" + day + "/" + year

system(str.toLatin1().data())

str = "date -s " + hour + ":" + min + ":" + "00"

system(str.toLatin1().data())

//强制写入到CMOS

system("clock -w")

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11792475.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存