Cảm ơn lời khuyên của bạn, nhưng tôi đã tìm thấy một giải pháp khác. Tôi đã viết my_timer lớp của riêng tôi mà chỉ đơn giản là có nó trên bộ đếm thời gian nội bộ thứ hai người ra mỗi giây. Trong cửa sổ chính của tôi, tôi kết nối thời gian chờ này với chức năng cập nhật hiển thị cho người dùng.
Các my_timer.cpp:
#include "my_timer.hpp"
my_timer::my_timer(QWidget *parent) : QTimer(parent)
{
notifier = new QTimer;
}
my_timer::~my_timer()
{
//...
}
QTimer* my_timer::get_notifier()
{
return notifier;
}
void my_timer::start(int msec)
{
QTimer::start(msec);
notifier->start(1000);
}
void my_timer::stop()
{
QTimer::stop();
notifier->stop();
}
Và trong main_window.cpp tôi:
void main_window::setup_connects()
{
// ...
connect(m_timer->get_notifier(), SIGNAL(timeout()), this, SLOT(on_update_label()));
// ...
}
void main_window::on_update_label()
{
if(m_timer->isActive())
{
if(remaining_secs > 1)
{
remaining_secs--;
}
else
{
remaining_secs = spin_box->value();
}
update_label();
}
}
void main_window::update_label()
{
m_time_string = QString("Remaining time until next execution: %1").arg(remaining_secs);
m_time_label->setText(m_time_string);
}
Có [REMAININGTIME] (http://qt-project.org/doc/qt-5.0/qtcore/qtimer.html#remainingTime-prop) tài sản trong Qt5 –