void DialogTest::slotGetCurrentTime()
{
QDateTime currentDateTime = QDateTime::currentDateTime();
ui.timeEdit->setTime(currentDateTime.time());
ui.dateEdit->setDate(currentDateTime.date());
ui.dateTimeEdit->setDateTime(currentDateTime);
ui.time->setText(currentDateTime.toString("hh:mm:ss"));
ui.date->setText(currentDateTime.toString("yyyy-MM-dd"));
ui.timedate->setText(currentDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
void DialogTest::slotSetTime()
{
QString str = ui.time->text();
QTime tm = QTime::fromString(str,"hh:mm:ss");
ui.timeEdit->setTime(tm);
}
void DialogTest::slotSetDate()
{
QString str = ui.date->text();
QDate date = QDate::fromString(str,"yyyy-MM-dd");
ui.dateEdit->setDate(date);
}
void DialogTest::slotSetDateTime()
{
QString str = ui.timedate->text();
str = str.trimmed();//去掉空格
QDateTime dt = QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss");
ui.dateTimeEdit->setDateTime(dt);
}
计算时间差:
QDateTime currentTime = QDateTime::currentDateTime();
QDateTime futureTime = currentTime.addSecs(10); // 在当前时间上增加10秒
int milliseconds = currentTime.msecsTo(futureTime);
qDebug() << "Milliseconds difference:" << milliseconds;