我如何使用 2 QTimer?一个用于计数,另一个用于显示
How can i use 2 QTimer? One is for counting and the other one is displaying
我想使用 Qt 的 C++ 定时器。我的代码中有一个计时器,但屏幕上的时间显示很慢。 timer -> start(500) 我猜必须每 20 秒更新一次。
我想为此使用 2 个计时器。一个用于更新和计时,另一个计时器将显示在屏幕上。我该怎么做,如果你能帮上忙我会很高兴
这是我的代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer,
SIGNAL(timeout()),
this,
SLOT(functionstart())
);
}
void MainWindow::functionStart()
{
int count;
count = ui->lcdNumber->value();
count++;
ui->lcdNumber->display(count);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive()){
timer->stop();
ui->pushButton->setText("Start");
}
else {
timer->start(500);
ui->pushButton->setText("Stop");
}
}
当你像这样启动一个计时器时 timer -> start(500) 它会每 500 毫秒(0.5 秒)超时一次,所以如果你想要一个 20 秒超时的计时器,你可以用 timer -> start(20000) 启动它。
现在,如果您想要另一个计时器来更新显示,您可以使用您喜欢的周期启动 timer2,并将其连接到另一个插槽以更新 UI,如下所示:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer, SIGNAL(timeout()), this, SLOT(functionStart()));
connect(timer2, SIGNAL(timeout()), this, SLOT(updateDisplay()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::functionStart()
{
count += 1;
}
void MainWindow::updateDisplay()
{
ui->lcdNumber->display(count);
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive() & timer2->isActive())
{
timer->stop();
timer2->stop();
ui->pushButton->setText("Start");
}
else
{
timer->start(1000);
timer2->start(20000);
ui->pushButton->setText("Stop");
}
}
我将计数定义移至 MainWindow 头文件以保持其值。
我为它添加了 qDebug 部分。当我这样做时,我可以在控制台上看到它每秒刷新 20 次。但是显示上仍然没有变化。这是我的代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
#include <QDebug>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer, SIGNAL(timeout()), this, SLOT(functionStart()));
connect(timer2, SIGNAL(timeout()), this, SLOT(updateDisplay()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::functionStart()
{
count += 1;
}
void MainWindow::updateDisplay()
{
ui->lcdNumber->display(count);
qDebug() << "asadsf";
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive() & timer2->isActive())
{
timer->stop();
timer2->stop();
ui->pushButton->setText("Start");
}
else
{
timer->start(40);
timer2->start(50);
ui->pushButton->setText("Stop");
}
}
我想使用 Qt 的 C++ 定时器。我的代码中有一个计时器,但屏幕上的时间显示很慢。 timer -> start(500) 我猜必须每 20 秒更新一次。
我想为此使用 2 个计时器。一个用于更新和计时,另一个计时器将显示在屏幕上。我该怎么做,如果你能帮上忙我会很高兴
这是我的代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer,
SIGNAL(timeout()),
this,
SLOT(functionstart())
);
}
void MainWindow::functionStart()
{
int count;
count = ui->lcdNumber->value();
count++;
ui->lcdNumber->display(count);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive()){
timer->stop();
ui->pushButton->setText("Start");
}
else {
timer->start(500);
ui->pushButton->setText("Stop");
}
}
当你像这样启动一个计时器时 timer -> start(500) 它会每 500 毫秒(0.5 秒)超时一次,所以如果你想要一个 20 秒超时的计时器,你可以用 timer -> start(20000) 启动它。 现在,如果您想要另一个计时器来更新显示,您可以使用您喜欢的周期启动 timer2,并将其连接到另一个插槽以更新 UI,如下所示:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer, SIGNAL(timeout()), this, SLOT(functionStart()));
connect(timer2, SIGNAL(timeout()), this, SLOT(updateDisplay()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::functionStart()
{
count += 1;
}
void MainWindow::updateDisplay()
{
ui->lcdNumber->display(count);
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive() & timer2->isActive())
{
timer->stop();
timer2->stop();
ui->pushButton->setText("Start");
}
else
{
timer->start(1000);
timer2->start(20000);
ui->pushButton->setText("Stop");
}
}
我将计数定义移至 MainWindow 头文件以保持其值。
我为它添加了 qDebug 部分。当我这样做时,我可以在控制台上看到它每秒刷新 20 次。但是显示上仍然没有变化。这是我的代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QPushButton>
#include <QDebug>
QTimer *timer = new QTimer();
QTimer *timer2 = new QTimer();
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(timer, SIGNAL(timeout()), this, SLOT(functionStart()));
connect(timer2, SIGNAL(timeout()), this, SLOT(updateDisplay()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::functionStart()
{
count += 1;
}
void MainWindow::updateDisplay()
{
ui->lcdNumber->display(count);
qDebug() << "asadsf";
}
void MainWindow::on_pushButton_clicked()
{
if(timer->isActive() & timer2->isActive())
{
timer->stop();
timer2->stop();
ui->pushButton->setText("Start");
}
else
{
timer->start(40);
timer2->start(50);
ui->pushButton->setText("Stop");
}
}