So I have a simple class below that uses a QTimer to add a number to the total every 1 second:
// 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();
}
During the loop, if I press if my input was 2, nothing is happening. The slot doesn't seem to fire as I don't see anything that is outputting to the console. I am pretty new to QT in general so I may have done something incorrect. Another thought I had that it could be due to some threading issues. But I haven't had much experience with threading before.