怎样用QT的 dateTimeEdit控件实现设置时间

怎样用QT的 dateTimeEdit控件实现设置时间,第1张

第一步,创建一个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")

}

声明没有定义,一般为头文件没有添加,查看发现你已经添加,但又发现#include <QDateTime>头文件上一行最后有个 “\” ,便变成了跟在上一行之后,出现错误,去掉 “\” 应该就没问题了

这个比较简单的哦,我给你讲步骤吧,

第一步,创建一个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/tougao/7791979.html

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

发表评论

登录后才能评论

评论列表(0条)

保存