Results 1 to 10 of 10

Thread: Emitting signals in other thread

  1. #1
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Emitting signals in other thread

    Hi,

    I have a requirement where I have to call QNetworkAccessManager methods from a class which has base class as QThread. And I also I have to capture QNetworkAccessManager's finished() signal.

    Qt Code:
    1. class test: public QThread{
    2. Q_OBJECT
    3. public:
    4. test(QObject *parent=0);
    5.  
    6. private:
    7. QNetworkAccessManager *manager
    8. }
    To copy to clipboard, switch view to plain text mode 

    when it emits signal by run() method in a class, it fails because it complains about conflict between parent thread and child thread.
    Need a solution to handle this


    Thanks

    Manish
    Last edited by wysota; 27th May 2011 at 19:15. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emitting signals in other thread

    Please show us some implementation code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Emitting signals in other thread

    You can signal between threads, if you read and follow the signals and slots docs carefully. But you need to understand that you can't, in a general sense, pass Qt objects (derived from QObject) between threads since they're "owned" by the thread where they're created.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emitting signals in other thread

    The problem is probably totally different. I'm expecting that the QNAM object is created in the constructor instead of the run() method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Emitting signals in other thread

    Please find attached code. Signal is not getting emitted in this and slot is not called.
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emitting signals in other thread

    You are trying to access a QObject (QNetworkReply) from a different thread (main thread, as this is where your thread object lives) than the thread it belongs to (the thread represented by your thread object where QNetworkAccessManager lives).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Emitting signals in other thread

    Ok, But what is way out for this. Hunted lot for solution but no luck.
    Please provide the solution to handle this situation

  8. #8
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Emitting signals in other thread

    connect() has an optional parameter. Perhaps you should use it. But keep in mind that the receiving thread must be running an "event loop" or nothing will happen.

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Emitting signals in other thread

    I think you have things in place, only miss these

    Qt Code:
    1. void mythread::run()
    2. {
    3. QNetworkAccessManager *manager= new QNetworkAccessManager();
    4. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(showData(QNetworkReply*)));
    5. QNetworkReply *rep = manager->get(QNetworkRequest(QUrl("http://www.google.co.in")));
    6. exec(); //Add this
    7. }
    8.  
    9. threadcross::~threadcross()
    10. {
    11. th.exit(0); //Add this, to make sure you stop and exit the thread
    12. while(th.isRunning()); //wait for the thread to exit, and then continue, actual thread will may take longer to finish and exit, hence this wait is required to avoid application crashing while closing. There are better ways to do this, you can figure them out eventually
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emitting signals in other thread

    Quote Originally Posted by mvbhavsar View Post
    Ok, But what is way out for this. Hunted lot for solution but no luck.
    Please provide the solution to handle this situation
    http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 22nd February 2011, 21:21
  2. Emitting signals from event handler
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2009, 07:26
  3. Replies: 3
    Last Post: 17th November 2009, 21:10
  4. emitting and catching signals
    By srohit24 in forum Qt Programming
    Replies: 12
    Last Post: 4th August 2009, 18:32
  5. emitting signals in const function
    By maxel in forum Qt Programming
    Replies: 3
    Last Post: 19th October 2008, 15:05

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.