以下方法分享按分钟更新时间,希望对大家有帮助
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()),this,SLOT(updateTime()));
if(horizontal)
{
QString currentTime = QDateTime::currentDateTime().toString("hh:mm");
time->setText(currentTime);
//logo->raise();
time->raise();
}
setTimer();
void View::setTimer()
{
int currentHour = QTime::currentTime().toString("h").toInt();
int currentMinutes = QTime::currentTime().toString("m").toInt();
int times;
if(currentMinutes == 59)
times = QTime::currentTime().msecsTo(QTime::fromString(tr("%1:%2:00").arg(currentHour+1).arg(0),"h:m:ss"));
else
times = QTime::currentTime().msecsTo(QTime::fromString(tr("%1:%2:00").arg(currentHour).arg(currentMinutes+1),"h:m:ss"));
qDebug()<<QTime::currentTime().toString("hh:m:ss")<<currentMinutes<<times;
timer->start(times);
}
void View::updateTime()
{
QString currentTime = QDateTime::currentDateTime().toString("hh:mm");
time->setText(currentTime);
setTimer();
}