【发布时间】:2018-11-24 14:29:34
【问题描述】:
我是 Qt C++ 的新手,从我在网上找到的少数资源中,我无法仅提取需要向表单添加倒数计时器的位。我不想添加任何按钮或其他功能。只需要有一个从 1:00 开始然后递减到 0:00 的计时器,此时我需要显示某种消息,指示用户时间到了。我想也许添加一个标签来显示计时器将是一种简单的方法(但现在确定我是否正确)。
到目前为止,我创建了一个新的 Qt 应用程序项目,在我的主窗体中添加了一个标签,并从我在 http://doc.qt.io/archives/qt-4.8/timers.html 获得的内容中添加了一些计时器代码到 mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Initialize "countdown" label text
ui->countdown->setText("1:00");
//Connect timer to slot so it gets updated
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(updateCountdown()));
//It is started with a value of 1000 milliseconds, indicating that it will time out every second.
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateCountdown()
{
//do something along the lines of ui->countdown->setText(....);
}
在 mainwindow.h 中,我添加了 QTimer *timer; 作为公共属性,还添加了 void updateCountdown(); 作为私有插槽。
但我不确定如何从这里继续。我认为下一步是每秒减少计时器并在“倒计时”标签上显示(这将在 updateCountdown 插槽上完成),但我不知道如何。 当倒计时到 0:00 时,我也不确定如何触发消息(可能在 QFrame 上)。
【问题讨论】: