- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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;
作为公共(public)属性,还添加了 void updateCountdown();
作为私有(private)槽。
但我不确定如何从这里继续下去。我认为下一步是每秒减少计时器并在“倒计时”标签上显示(这将在 updateCountdown 插槽上完成)但我不知道如何做。我也不确定如何在倒计时到达 0:00 时触发消息(可能在 QFrame 上)。
最佳答案
来自 QTimer documentation ,函数 updateCountdown()
在您的配置中每 1 秒调用一次。因此,每次调用此函数并在 UI 中更新时,您都应该从计时器中减少一秒。目前您没有将时间存储在任何地方,所以我建议您将其添加为全局暂时,例如QTime time(0, 1, 0)
QTime Documentation .
然后在 updateCountdown()
中,调用 time.addSecs(-1);
然后 ui->countdown->setText(time.toString("m:ss"));
。然后很容易检查它是否为“0:00”并执行其他操作。
希望对你有帮助
关于c++ - Qt C++ : how to add a simple countdown timer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459173/
我正在尝试使用 Countdown.js 。我发现this guide并复制代码并粘贴它以尝试调整和理解代码,但是当我在 my web 中运行它时我无法让它工作,在控制台中我得到“Uncaught R
我正在尝试构建一个 polymer 倒计时小部件。 在 Javascript 中,我会为此编写一个类似的函数,并不断将更新的倒计时时间插入到所需的 ID 元素中。 setInterval(funct
我不断收到“倒计时未定义”的消息 var timeout = setTimeout('countdown()',1000); 它应该每秒循环一次函数 countdown()。我该如何解决这个问题?
我的代码中有这一行 $('#timeLeftDiv').countdown({ until: expires, timezone: 0, serverSync: serverTime, onTick:
我很难处理 JqueryCountdown 脚本样式。这是视觉上发生的事情...... 这是用完 visual studios 调试器后的样子 我希望样式看起来更像第二个而不是第一个。我不是很擅长 C
我是C#的新手,正在尝试使用System.Timer.Timers创建一个简单的倒数计时器。它没有按预期工作,我在互联网上搜索了解决方案,但并没有真正解决我的问题。我想要的是当用户单击开始按钮时,它开
我正在使用 http://countdownjs.org/ 中的非常好的脚本几乎一切都运转良好。但是,我想要没有字符串 小时、分钟、秒的格式 - 只是一个普通的计时器(hh:mm:ss)。我正在考虑使
您好,我尝试按一个按钮从 countdown.js 启动和重置计数器。 当我第二次按下按钮时,它会启动一个新计时器并在旧计时器和新计时器之间跳转。 我想清除计时器/重置计时器并开始。 我尝试使用cle
为什么以下不起作用? Example Time to start: Main.js: function setT
我正在尝试创建一个与倒计时电视节目相关的简单程序。我才刚刚开始,所以我的问题从一开始就存在。当我要求用户输入 c 或 v 作为辅音或元音时,我只是检查它是否返回 yes、no 或请重试。这有效,但它直
我正在使用 https://bitbucket.org/mckamey/countdown.js用于在 fromDate 和 toDate 之间设置倒计时文本。下面是我的代码片段 var fromDa
我正在从 MySql 数据库检索 DateTime,但它的格式与倒计时允许的格式不同。 mysql has this date format: 2015-07-07 00:00:00 countdow
我正在使用 countdown.js: http://blog.smalldo.gs/2013/12/create-simple-countdown/ 我没有使用 JavaScript 的经验,我需要
我正在使用 JQUERY.COUNTDOWN - 我有一个问题: 可以使用多个实例并在倒计时结束时隐藏表格行吗? 我有一个表格,每一行都有一个像这样的“倒计时”: ------------------
我对这个脚本有疑问 - var date = new Date(new Date().valueOf() + 60 * 1000); $('#clock').countdown(date)
尝试使用 this 实现 jQuery 倒计时引用 代码: Merry Christmas var newYear
我正在尝试编写一个程序,在单击按钮时打开一个表单。此表单有一个带有倒计时的标签。主窗体有一个按钮,它执行以下操作: private void btnOpen_Click(object sender,
我正在创建一个按天、小时和秒计算的倒数计时器。我目前将它们存储在 String 数组中,同时还将它们转换为 Int(使用 map() ),以便在将值放入标签时可以倒计时/递减。 打印到日志时的当前输出
请帮忙。我想使用 iphone sdk 创建倒数计时器。我有当前时间: NSDate *today = [NSDate date]; NSDateFormatter *dateFormatter =
我知道我应该了解 Apple 的 NSTimer 文档,但我没有!我也阅读了很多关于它的问题,但找不到适合我的问题。那么这里是: 用户通过文本字段输入小时和分钟。我将它们转换为整数并将它们显示在“co
我是一名优秀的程序员,十分优秀!