PDA

View Full Version : signals emitted but slot dont call



danics
6th November 2013, 20:18
Hi,
i have a object in my UI thread and i send a pointer of this object to another thread then i emit a signal of object but connected slot don't called:



class MyObject : public QObject {
...
void checkMyEvents() [
...
emit processfinished();
...
}

void start() {
...
othread->start();
...
}
signals:
void processfinished();
private:
MyThread* otherad;
}

class MyThread : public QThread
{
public:
void run() {
...
obj->checkMyEvents();
...
}

MyObject* obj;
}

main() {
....
MyObject obj;
QObject::connect(&obj, SIGNAL(processfinished()), &anotherObj, SLOT(showresults());
....
}


in debug mode i can see signals emitted but slot don't call :(
whats wrong in my code?

ChrisW67
6th November 2013, 21:00
whats wrong in my code?
Hard to say... you show very little of it. The little we can see... the "obj" at line 34 is not related to the "obj" at lines 25 and 29, and nothing indicates how the latter gets a value.

spirit
7th November 2013, 08:09
Did you forget to add Q_OBJECT?

danics
8th November 2013, 06:46
no i dont forget to add Q_OBJECT,
i send this argument to MyThread in constructor of MyObject:
othread = new MyThread(this);

stampede
8th November 2013, 07:21
i send this argument to MyThread in constructor
So you have a signal emitted from constructor ? How this could possibly work, if you call "connect" after signal has been emitted...
Maybe I'm wrong. Show us more code, maybe a compilable example.

danics
8th November 2013, 09:48
no as you can see othread start in start function of MyObject

Added after 6 minutes:


the "obj" at line 34 is not related to the "obj" at lines 25 and 29, .
i send this argument of MyObject to MyThread in constructor of MyObject:
othread = new MyThread(this);

you have a signal emitted from constructor ?
no as you can see othread start in start function of MyObject

stampede
8th November 2013, 09:52
Only thing I can see is that some connect is called after you create an instance of MyObject. I don't know when start() of MyObject is called. I can only guess, but if it is in the constructor, then you can have the signal "processFinished" emitted before calling "connect" on it. If only thing called in the Threads "run" method is the emit statement, then you have a perfect example of a single threaded processing.
Show us full code or we will guess like this for next 1000 posts.

Look, this is what I mean:


// not multi-threaded

#include <QThread>
#include <QDebug>
#include <QCoreApplication>

class Thread : public QThread{
Q_OBJECT
public:
Thread(QObject * parent = NULL) : QThread(parent){

}
void run(){
qDebug() << "thread in run(): " << QThread::currentThread();
emit signal1();
}
signals:
void signal1();
};

class MyObject : public QObject{
Q_OBJECT
public:
MyObject(QObject * parent = NULL) : QObject(parent)
{
Thread * thread = new Thread(this);
connect(thread, SIGNAL(signal1()), this, SLOT(aSlot()));
thread->run();
}
public slots:
void aSlot(){
qDebug() << "A SLOT: " << QThread::currentThread();
}
};

int main(int argc, char ** argv){
QCoreApplication app(argc,argv);
qDebug() << "thread in main(): " << QThread::currentThread();
MyObject obj;
qDebug() << "before exec()";
return 0;
}