C++ QThread and connect causing crashes

Multi tool use
Multi tool use


C++ QThread and connect causing crashes



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





@eyllanesc its just a sleep function from windows.h
– mrdeadguy34
Jul 1 at 9:41





what is Count->Seconds??
– eyllanesc
Jul 1 at 9:42


Count->Seconds





@eyllanesc class Counter : public QObject { Q_OBJECT public slots: void Process(); signals: void finished(); void SecondsUpdate(); public: int getValue() { return Seconds;} int Seconds; };
– mrdeadguy34
Jul 1 at 9:42





please edit your question and add it there.
– eyllanesc
Jul 1 at 9:43





@eyllanesc done
– mrdeadguy34
Jul 1 at 9:45




2 Answers
2



The code shown has two basic problems.


Counter::Seconds


Count


QThread



You could solve both of these by getting rid of the Seconds member and just passing the local counter secs as a parameter to the Counter::SecondsUpdate signal...


Seconds


secs


Counter::SecondsUpdate


class Counter: public QObject
{
Q_OBJECT;
public slots:
void Process();
signals:
void finished();
void SecondsUpdate(int seconds);
public:
};

void Counter::Process ()
{
int secs = 0;
while (secs < 1000)
{
qDebug() << "hello" << secs;
secs += 1;

/*
* Sleep(1000) --> QThread::sleep(1)
*/
QThread::sleep(1);

/*
* Pass secs as a parameter to the SecondsUpdate signal.
*/
emit SecondsUpdate(secs);
}
emit finished();
}



Then, change the relevant connect call from...


connect


connect(Count, &Counter::SecondsUpdate, this, [=]{ SetLabel(Count->Seconds) ;});



to...


connect(Count, &Counter::SecondsUpdate, this, &BaseWindow::SetLabel);





change connect(Count, &Counter::SecondsUpdate, this, [=](int seconds) { SetLabel(seconds); }); to connect(Count, &Counter::SecondsUpdate, this, &BaseWindow::SetLabel), is more elegant
– eyllanesc
Jul 1 at 10:52



connect(Count, &Counter::SecondsUpdate, this, [=](int seconds) { SetLabel(seconds); });


connect(Count, &Counter::SecondsUpdate, this, &BaseWindow::SetLabel)





@eyllanesc Good point, thanks. Fixed.
– G.M.
Jul 1 at 11:02





Thanks for your reply. Looks great to me!
– mrdeadguy34
Jul 1 at 14:32



Fixed by passing through the label as a pointer and edited it in the function.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

pIVcziYegfW 7e6U ak6NBG6U,1HukVWc SP
Usepyskksg40LPBvfOn,HuniF7P2mi,F8EFEhrR

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask