我需要创建一个在 QTCreator 中启动计时器的按钮
创建一个计时器,该计时器在按钮处于活动状态时启动,在按钮关闭时暂停。 为了能够继续我的项目,ainda estou aprendendo como funciona o QTCreator
好吧,你的问题的一部分是葡萄牙语,但我明白,毕竟我也是巴西人。
在
Qt Creator
表格中,您需要添加 QPushButton
和 QTimer
。
在
Qt Creator
表单编辑模式下,右键单击按钮并选择 Slots
-> clicked()
。
cpp代码:
#include <QMainWindow>
#include <QPushButton>
#include <QTimer>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
{
QPushButton *button = new QPushButton("Start/Stop Timer", this);
connect(button, &QPushButton::clicked, this, &MainWindow::handleButtonClick);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::handleButtonClick); // Connect to start/stop button when timer expires
}
private slots:
void handleButtonClick()
{
if (timer->isActive()) {
timer->stop();
} else {
timer->start(1000); // 1000 ms interval, adjust as needed
}
}
private:
QTimer *timer;
};