My QThread counter crashes giving odd results for what the number should be as the Thread counts properly but in the SetLabel function I get a different number to whats in the QThread ands it then crashes after 3 seconds and the label doesnt seem to update.
QThread* CountThread = new QThread;
Counter* Count = new Counter();
Count->moveToThread(CountThread);
connect(CountThread, SIGNAL(started()), Count, SLOT(Process()));
connect(Count, &Counter::SecondsUpdate, this, [=]{ SetLabel(Count->Seconds) ;});
connect(Count, SIGNAL(finished()), CountThread, SLOT(quit()));
CountThread->start();
void Counter::Process()
{
int secs = 0;
while (secs < 1000)
{
qDebug() << "hello" << secs;
secs += 1;
Sleep(1000);
emit SecondsUpdate();
}
emit finished();
}
void BaseWindow::SetLabel(int Seconds)
{
qDebug() << Seconds;
this->Test->setText(QString("Seconds: " + QString::number(Seconds)));
}
class Counter : public QObject
{
Q_OBJECT
public slots:
void Process();
signals:
void finished();
void SecondsUpdate();
public:
int getValue() { return Seconds;}
int Seconds;
};
EDIT: The issue seems to lie in the changing of the label as I commented this->Text->setText out and it didnt crash