Monitoring Qt GUI from QThread class

I am trying to start a background thread (qthread) which should control a flag in gui and it will not work! It is created, but at runtime I get this error:

"Unhandled exception at 0x0120f494 in program.exe: 0xC0000005: read location of access violation 0xcdcdce55."

and it breaks into the string "connect". What is the best way to do this?

guiclass::guiclass(){
    thread *t = new thread();
}

thread::thread(){
     guiclass *c = new guiclass();
     connect(c->checkBox, SIGNAL(stateChanged(int)), this, SLOT(checked(int)));

     ....
     start work
     ....
}

bool thread::checked(int c){
     return(c==0);
}

void thread::run(){

    if(checked()){
        do stuff
    }
}
+3
source share
1 answer

QThread , , . - "" ( QObject), , moveToThread, .

:

class ObjectThatMonitorsCheckbox : public QObject
{
     Q_OBJECT
     // ...

public slots:
     void checkboxChecked(int checked);
}

, :

QThread myWorkerThread;

ObjectThatMonitorsCheckbox myHandlerObject;

myHandlerObject.moveToThread(&myworkerThread);
connect(c->checkBox, SIGNAL(stateChanged(int)), &myHandlerObject, 
    SLOT(checkboxChecked(int)));

myWorkerThread.start();

: QThread - .

, !

: Qt: QThread?

+3

All Articles