Results 1 to 7 of 7

Thread: signals emitted but slot dont call

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default signals emitted but slot dont call

    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:

    Qt Code:
    1. class MyObject : public QObject {
    2. ...
    3. void checkMyEvents() [
    4. ...
    5. emit processfinished();
    6. ...
    7. }
    8.  
    9. void start() {
    10. ...
    11. othread->start();
    12. ...
    13. }
    14. signals:
    15. void processfinished();
    16. private:
    17. MyThread* otherad;
    18. }
    19.  
    20. class MyThread : public QThread
    21. {
    22. public:
    23. void run() {
    24. ...
    25. obj->checkMyEvents();
    26. ...
    27. }
    28.  
    29. MyObject* obj;
    30. }
    31.  
    32. main() {
    33. ....
    34. MyObject obj;
    35. QObject::connect(&obj, SIGNAL(processfinished()), &anotherObj, SLOT(showresults());
    36. ....
    37. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: signals emitted but slot dont call

    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.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: signals emitted but slot dont call

    Did you forget to add Q_OBJECT?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: signals emitted but slot dont call

    no i dont forget to add Q_OBJECT,
    i send this argument to MyThread in constructor of MyObject:
    othread = new MyThread(this);

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: signals emitted but slot dont call

    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.

  6. #6
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: signals emitted but slot dont call

    no as you can see othread start in start function of MyObject


    Added after 6 minutes:


    Quote Originally Posted by ChrisW67 View Post
    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);
    Quote Originally Posted by ChrisW67;
    you have a signal emitted from constructor ?
    no as you can see othread start in start function of MyObject
    Last edited by danics; 8th November 2013 at 09:48.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: signals emitted but slot dont call

    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:
    Qt Code:
    1. // not multi-threaded
    2.  
    3. #include <QThread>
    4. #include <QDebug>
    5. #include <QCoreApplication>
    6.  
    7. class Thread : public QThread{
    8. Q_OBJECT
    9. public:
    10. Thread(QObject * parent = NULL) : QThread(parent){
    11.  
    12. }
    13. void run(){
    14. qDebug() << "thread in run(): " << QThread::currentThread();
    15. emit signal1();
    16. }
    17. signals:
    18. void signal1();
    19. };
    20.  
    21. class MyObject : public QObject{
    22. Q_OBJECT
    23. public:
    24. MyObject(QObject * parent = NULL) : QObject(parent)
    25. {
    26. Thread * thread = new Thread(this);
    27. connect(thread, SIGNAL(signal1()), this, SLOT(aSlot()));
    28. thread->run();
    29. }
    30. public slots:
    31. void aSlot(){
    32. qDebug() << "A SLOT: " << QThread::currentThread();
    33. }
    34. };
    35.  
    36. int main(int argc, char ** argv){
    37. QCoreApplication app(argc,argv);
    38. qDebug() << "thread in main(): " << QThread::currentThread();
    39. MyObject obj;
    40. qDebug() << "before exec()";
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 8th November 2013 at 10:08.

Similar Threads

  1. Problem with the signals emitted in Phonon
    By Valsylver in forum Qt Programming
    Replies: 3
    Last Post: 30th July 2012, 07:30
  2. QAction, triggered signal dont call a slot
    By kaszewczyk in forum Newbie
    Replies: 6
    Last Post: 5th October 2010, 21:30
  3. [SOLVED] Two signals emitted, only one slot called!
    By codeverse in forum Qt Programming
    Replies: 0
    Last Post: 11th August 2010, 15:46
  4. Replies: 1
    Last Post: 7th December 2009, 18:49
  5. Replies: 2
    Last Post: 16th August 2007, 00:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.