传递当前时间变量
passing a current time variable
我正在尝试使用 QTimer 让文本编辑框每 5 秒显示一次当前时间。我在一个单独的方法中计算当前时间,然后让 QTimer 调用该方法并显示当前时间。我一辈子都想不出如何将变量从 setCurrentTime 方法传递给 QTimer。我确定这真的很容易解决,但我无法弄清楚。这是我的代码。
void noheatmode::setCurrentTime()
{
QTime time = QTime::currentTime();
QString sTime = time.toString("hh:mm:mm");
// ui->tempTimeNoHeatMode->append(sTime);
}
void noheatmode::on_timeButton_clicked()
{
QTimer *timer =new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
timer->start(5000);
ui->tempTimeNoHeatMode->append(sTime);
}
如果我答对了你的问题,你只有分钟变量而不是秒。只需将 "hh:mm:mm" 更改为 "hh:mm:ss"
void noheatmode::setCurrentTime()
{
QTime time = QTime::currentTime();
QString sTime = time.toString("hh:mm:ss");
ui->tempTimeNoHeatMode->append(sTime);
}
使用您的代码:
void noheatmode::on_timeButton_clicked()
{
QTimer *timer =new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
timer->start(5000);
ui->tempTimeNoHeatMode->append(sTime);
}
这意味着 SLOT
中的函数将每 5000
毫秒调用一次,即 5 秒。那么可以做的是将函数 setCurrentTime()
设置为在每次调用时更新文本框。
示例:
void Class::setCurrentTime()
{
QTime times = (QTime::currentTime());
QString currentTime=times.toString("hh:mm:ss");
ui->label->setText(currentTime);
//Assuming you are using a label to output text, else substitute for what you are using instead
//Every time this function is called, it will receive the current time
//and update label to display the time received
}
我正在尝试使用 QTimer 让文本编辑框每 5 秒显示一次当前时间。我在一个单独的方法中计算当前时间,然后让 QTimer 调用该方法并显示当前时间。我一辈子都想不出如何将变量从 setCurrentTime 方法传递给 QTimer。我确定这真的很容易解决,但我无法弄清楚。这是我的代码。
void noheatmode::setCurrentTime()
{
QTime time = QTime::currentTime();
QString sTime = time.toString("hh:mm:mm");
// ui->tempTimeNoHeatMode->append(sTime);
}
void noheatmode::on_timeButton_clicked()
{
QTimer *timer =new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
timer->start(5000);
ui->tempTimeNoHeatMode->append(sTime);
}
如果我答对了你的问题,你只有分钟变量而不是秒。只需将 "hh:mm:mm" 更改为 "hh:mm:ss"
void noheatmode::setCurrentTime()
{
QTime time = QTime::currentTime();
QString sTime = time.toString("hh:mm:ss");
ui->tempTimeNoHeatMode->append(sTime);
}
使用您的代码:
void noheatmode::on_timeButton_clicked()
{
QTimer *timer =new QTimer(this);
connect(timer,SIGNAL(timeout()), this, SLOT(setCurrentTime()));
timer->start(5000);
ui->tempTimeNoHeatMode->append(sTime);
}
这意味着 SLOT
中的函数将每 5000
毫秒调用一次,即 5 秒。那么可以做的是将函数 setCurrentTime()
设置为在每次调用时更新文本框。
示例:
void Class::setCurrentTime()
{
QTime times = (QTime::currentTime());
QString currentTime=times.toString("hh:mm:ss");
ui->label->setText(currentTime);
//Assuming you are using a label to output text, else substitute for what you are using instead
//Every time this function is called, it will receive the current time
//and update label to display the time received
}