Results 1 to 9 of 9

Thread: QThread signal and slots problem

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread signal and slots problem

    Hello,
    I don't know why slot is not working...

    k.h
    Qt Code:
    1. class logoThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. logoThread(QString param1, QString param2) { strUrl = param1; QString strChannel = param2; }
    6.  
    7. void run()
    8. {
    9. QTimer::singleShot(0, this, SLOT(doTheWork()));
    10.  
    11. exec();
    12. }
    13.  
    14. public slots:
    15. void doTheWork()
    16. {
    17. QNetworkAccessManager accessManager;
    18. QNetworkReply* pReply;
    19. QEventLoop eventLoop;
    20. pReply = accessManager.get(QNetworkRequest(QUrl(strUrl)));
    21. QObject::connect(pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    22. eventLoop.exec();
    23.  
    24. QByteArray bData = pReply->readAll();
    25.  
    26. delete pReply;
    27. qDebug() << "got img";
    28. emit setLogo(strChannel, bData);
    29.  
    30. quit(); // not needed ?
    31. }
    32.  
    33. private:
    34. QString strUrl;
    35. QString strChannel;
    36.  
    37. signals:
    38. void setLogo(QString, QByteArray);
    39.  
    40. };
    41.  
    42. class k : public QObject
    43. {
    44. Q_OBJECT
    45. public:
    46. ...
    47.  
    48. public slots:
    49. void setLogoS(QString, QByteArray);
    50. }
    To copy to clipboard, switch view to plain text mode 
    k.cpp
    Qt Code:
    1. void k::img()
    2. {
    3. ....
    4. logoThread *logoThr = new logoThread(strU, strC);
    5. QObject::connect(logoThr, SIGNAL(setLogo(QString,QByteArray)), this, SLOT(setLogoS(QString,QByteArray)), Qt::QueuedConnection);
    6. logoThr->start();
    7. }
    8. }
    9. }
    10. }
    11.  
    12. void k::setLogoS(QString param1, QByteArray param2)
    13. {
    14. qDebug() << "slot";
    15. // show img
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QThread signal and slots problem

    I guess the slot is called, but you don't receive any data. That's because get() works asynchronously! Read on how to use QNetworkAccessManager correctly. (Your posted thread class looks also very very crowded and "ugly"!)

  3. #3
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread signal and slots problem

    I recieve data, but slot is not called.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QThread signal and slots problem

    I used your code (only removed the network things):
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4.  
    5. class logoThread : public QThread
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. logoThread(QString param1, QString param2)
    11. {
    12. strUrl = param1;
    13. strChannel = param2;
    14. }
    15.  
    16. void run()
    17. {
    18. QTimer::singleShot(0, this, SLOT(doTheWork()));
    19. exec();
    20. }
    21.  
    22. public slots:
    23. void doTheWork()
    24. {
    25. qDebug() << "got img";
    26. QByteArray bData;
    27. emit setLogo(strChannel, bData);
    28. quit(); // not needed ?
    29. }
    30.  
    31. private:
    32. QString strUrl;
    33. QString strChannel;
    34.  
    35. signals:
    36. void setLogo(QString, QByteArray);
    37. };
    38.  
    39. class k : public QObject
    40. {
    41. Q_OBJECT
    42. public:
    43. k(QObject* parent = 0) : QObject(parent) {}
    44.  
    45. public slots:
    46. void setLogoS(QString, QByteArray)
    47. {
    48. qDebug() << __FUNCTION__ << "reached";
    49. }
    50. };
    51.  
    52. int main(int argc, char** argv)
    53. {
    54. QApplication app(argc, argv);
    55.  
    56. k test;
    57. logoThread* logoThr = new logoThread("foo", "bar");
    58. QObject::connect(logoThr, SIGNAL(setLogo(QString, QByteArray)), &test, SLOT(setLogoS(QString, QByteArray)), Qt::QueuedConnection);
    59. logoThr->start();
    60.  
    61. return app.exec();
    62. }
    63.  
    64.  
    65. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    and the slot is called! So the error must be somewhere else in your original code. Try to strip your code down to locate the error.

  5. #5
    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: QThread signal and slots problem

    The slot will be called in context of the main thread, not the thread represented by the QThread object which is probably not what you want as your thread is otherwise idle. If you main thread is blocked, the slot will not be called.
    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.


  6. #6
    Join Date
    Apr 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: QThread signal and slots problem

    Quote Originally Posted by wysota View Post
    The slot will be called in context of the main thread, not the thread represented by the QThread object which is probably not what you want as your thread is otherwise idle. If you main thread is blocked, the slot will not be called.
    I agree with what wysota said. I met the same proble with using a thread to connect with Http server. And slots are called ,but reply->readAll() return empty. That issues makes me almost crazy.
    Finally, I rethink about if thread really send out request.
    I think NetworkAccessManager just put data and request into a queue which only main thread can send it out. So I recommand you use a QTimer in your class logoThread instead of thread ,and if so, the request can been sent and datas can be read correctly.I did so , and got datas from Http server. I think that may help you.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread signal and slots problem

    Have you looked at the documentation for moveToThread?

  8. #8
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread signal and slots problem

    Quote Originally Posted by fatjuicymole View Post
    Have you looked at the documentation for moveToThread?
    yes, but I don't know where use it ;/

  9. #9
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread signal and slots problem

    Quote Originally Posted by Lykurg View Post
    I used your code (only removed the network things): and the slot is called! So the error must be somewhere else in your original code. Try to strip your code down to locate the error.
    Thank you - Yes, error was somewhere else in code.

Similar Threads

  1. Queuing problem with signal and slots
    By montylee in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2009, 06:11
  2. QTcpServer and QThread signal/slot problem
    By Diph in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 19:34
  3. QThread communication without signal and slots
    By forrestfsu in forum Qt Programming
    Replies: 16
    Last Post: 22nd May 2007, 00:43
  4. Signal-slots connection problem.
    By impeteperry in forum Qt Tools
    Replies: 26
    Last Post: 23rd January 2007, 14:33
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51

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.