[英]How to stop a QTimer?
所以我在下面有一個簡單的類,它使用 QTimer 每 1 秒向總數添加一個數字:
// score.h
#include <QObject>
#include <QTimer>
class Score : public QObject
{
Q_OBJECT
public:
explicit Score(QObject *parent = nullptr);
void start();
void stop();
QTimer *timer;
int getScore();
private slots:
void update();
private:
int score;
};
// score.cpp
#include "score.h"
Score::Score(QObject *parent) : QObject(parent)
{
score = 0;
timer = new QTimer(this);
}
void Score::start()
{
timer->start(1000);
}
void Score::stop()
{
timer->stop();
}
int Score::getScore()
{
return score;
}
void Score::update()
{
score += 10;
qDebug() << score;
}
// main.cpp
#include <QCoreApplication>
#include "score.h"
#include <QtDebug>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Score score{};
std::string input{""};
while(input!="1") {
std::cout << "1. to stop" << std::endl;
std::cout << "2. to start" << std::endl;
std::cin >> input;
if(input=="1") {
score.stop();
}
else if(input=="2") {
score.start();
}
}
return a.exec();
}
在循環過程中,如果我按輸入為 2,則不會發生任何事情。 該插槽似乎沒有觸發,因為我沒有看到任何輸出到控制台的內容。 總的來說,我對 QT 還是很陌生,所以我可能做錯了一些事情。 我的另一個想法是,這可能是由於一些線程問題。 但我之前沒有太多線程經驗。
您的實現有 2 個關鍵錯誤:
eventloop 永遠不會啟動,因為 while 循環不允許它,如果 eventloop 沒有啟動,那么異步元素如 QTimer、信號等將不起作用。
即便如此,事件循環仍然有效。 您尚未在超時信號與更新槽之間建立連接。
考慮到上述情況,您必須在不使用循環的情況下實現 stdin 的讀取,並且讀取的實現將取決於操作系統,因此為了簡化解決方案,我們可以使用QConsole 庫通過信號實現 stdin 的讀取:
├── 3rdParty
│ └── QConsole
│ ├── Demo
│ │ ├── Demo.pro
│ │ └── main.cpp
│ ├── LICENSE
│ ├── qconsole.cpp
│ ├── qconsole.h
│ ├── qconsole.pri
│ ├── README.md
│ ├── readthread_win.cpp
│ └── readthread_win.h
├── 64746801.pro
├── main.cpp
├── score.cpp
└── score.h
* .pro
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
SOURCES += \
main.cpp \
score.cpp
HEADERS += \
score.h
include(3rdParty/QConsole/qconsole.pri)
主程序
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <qconsole.h>
#include "score.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Score score;
QConsole console;
QObject::connect(&console, &QConsole::readyRead, [&](){
auto data = console.read(console.bytesAvailable());
if(data == QStringLiteral("1\n")){
score.stop();
}
else if(data == QStringLiteral("2\n")){
score.start();
}
});
if(!console.open()) {
qCritical() << console.errorString();
return EXIT_FAILURE;
}
return a.exec();
}
分數.h
#ifndef SCORE_H
#define SCORE_H
#include <QObject>
class QTimer;
class Score : public QObject
{
Q_OBJECT
public:
explicit Score(QObject *parent = nullptr);
void start();
void stop();
int getScore();
private slots:
void update();
private:
int score;
QTimer *timer;
};
#endif // SCORE_H
分數.cpp
#include "score.h"
#include <QTimer>
#include <QDebug>
Score::Score(QObject *parent) : QObject(parent)
{
score = 0;
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Score::update);
}
void Score::start()
{
timer->start(1000);
}
void Score::stop()
{
timer->stop();
}
int Score::getScore()
{
return score;
}
void Score::update()
{
score += 10;
qDebug() << score;
}
完整的例子在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.