Results 1 to 4 of 4

Thread: Can't receive signal from another thread.

  1. #1

    Default Can't receive signal from another thread.

    Can't receive signal from another thread as following.
    Using connect type as Qt::BlockingQueuedConnection, the thread blocks. But signal not received.

    main.cpp
    Qt Code:
    1. #include "myclass.h"
    2.  
    3.  
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10.  
    11. MyClass myClass;
    12. myClass.startAll();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    thread.h
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QThread>
    3.  
    4. #include <QDebug>
    5.  
    6.  
    7.  
    8. class Thread1 : public QThread
    9. {
    10. Q_OBJECT
    11. public:
    12. void run( void )
    13. {
    14. qDebug() << "thread 1 started";
    15. int i = 0;
    16. while(1)
    17. {
    18. msleep( 10 );
    19. i++;
    20. qDebug() << "i is: " + QString::number(i);
    21. if(i==1000)
    22. {
    23. qDebug() << "Got I";
    24. emit MyThread1Signal();
    25. exit();
    26. }
    27. }
    28. }
    29. signals:
    30. void MyThread1Signal();
    31. };
    To copy to clipboard, switch view to plain text mode 




    myclass.cpp
    Qt Code:
    1. MyClass::MyClass(QObject *parent) :
    2. QObject(parent)
    3. {
    4. thread1 = new Thread1();
    5. connect(thread1, SIGNAL(MyThread1Signal()), this, SLOT(myClassSlot1()), Qt::BlockingQueuedConnection);
    6. }
    7.  
    8.  
    9. void MyClass::startAll()
    10. {
    11. thread1->start();
    12. thread1->wait();
    13. }
    14.  
    15.  
    16. void MyClass::myClassSlot1()
    17. {
    18. qDebug()<<"Signal received";
    19. }
    To copy to clipboard, switch view to plain text mode 

    myclass.h
    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. #include <QObject>
    5.  
    6. #include "threads.h"
    7.  
    8. class MyClass : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit MyClass(QObject *parent = 0);
    13.  
    14. void startAll();
    15.  
    16. signals:
    17.  
    18. public slots:
    19. void myClassSlot1();
    20.  
    21. private:
    22. Thread1* thread1;
    23. };
    24.  
    25. #endif // MYCLASS_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't receive signal from another thread.

    Because You never launch main event loop. Why wait for the end of the second thread in the method MyClass::startAll calling thread1->wait() ?

  3. #3

    Default Re: Can't receive signal from another thread.

    Hi,

    For It needs not receive any event in thread, so I don't think exec() is required.


    Remove wait(), it is ok. It's this wait() that makes the thread never give up execution. Right?

    Please correct me if my understanding is wrong.
    Last edited by useryy; 14th December 2011 at 07:54.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't receive signal from another thread.

    Yes, a problem is wait(). But I'm talking about main exec. You have a connection with attribute Qt::BlockingQueuedConnection. This means that the event is delivered through the recipient's message queue thread. This is not the same as Qt:: DirectConnection.

Similar Threads

  1. How to signal when a Qwidget receive focus
    By franco.amato in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2016, 16:33
  2. Replies: 6
    Last Post: 5th February 2011, 20:18
  3. receive signal from a thread of an external program
    By bigbill in forum Qt Programming
    Replies: 5
    Last Post: 1st December 2010, 22:41
  4. not able to receive SIGNAL
    By thefriedc in forum Newbie
    Replies: 4
    Last Post: 28th September 2010, 08:34
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.