Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: QTcpSocket: no readyRead() signal even in Qthread event loop

  1. #21
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QTcpSocket: no readyRead() signal even in Qthread event loop

    Quote Originally Posted by wysota View Post
    The main thread also has (or at least can have) an event loop
    In application (plugin) where it doesn't work, call to ClientSocked takes place not from the main thread. And in example above, where I use ClientSocket from main(), it works.
    Quote Originally Posted by wysota View Post
    Sure, but you are calling the socket's functions directly from the main thread which is forbidden.
    You are right, connect and write had to be called also in run in this example.

  2. #22
    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: QTcpSocket: no readyRead() signal even in Qthread event loop

    No, connect() can be called anywhere. The point is not to call any of the methods of objects belonging to thread B from thread A. Regardless of whether there are slots involved anywhere.

    So this is forbidden:
    Qt Code:
    1. ClientSocket* c=new ClientSocket();
    2. c->start();
    3. Sleep(10000); // doesn't make sense
    4. bool bConnected = c->connectToHost("localhost", 1000); // forbidden
    To copy to clipboard, switch view to plain text mode 

    and if you change those calls to proper ones (using signals and slots), you'll notice there is no advantage of having a thread anywhere, it only causes trouble. Qt works in asynchronous fashion, when you try to force it to be synchronous (like in your code above), you'll have more problems with it than benefits from not having to switch your thinking into asynchronous mode.
    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. #23
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QTcpSocket: no readyRead() signal even in Qthread event loop

    Beow is "true" asynchronous version where communication takes place only via signals and slots (no threads!):
    Qt Code:
    1. class ClientSocket : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ClientSocket() : QObject() , m_socket(new QTcpSocket(this)) {
    6. connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()));
    7. }
    8. virtual ~ClientSocket() {};
    9. bool connectToHost(QString host, int port) {
    10. m_socket->connectToHost(host, port);
    11. return m_socket->waitForConnected(1000);
    12. }
    13. void write(const char* Data,unsigned int nBytes) {
    14. if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState))
    15. {
    16. m_socket->write(Data);
    17. m_socket->flush();
    18. }
    19. }
    20.  
    21. public slots:
    22. void writeData(const char* Data,unsigned int nBytes) { write(Data, nBytes); }
    23. void connectSlot(const QString& host, int port) { connectToHost(host, port); }
    24. void test() { throw new std::runtime_error("ok"); }
    25. private:
    26. QTcpSocket* m_socket;
    27. };
    To copy to clipboard, switch view to plain text mode 
    Here is the calling part:
    Qt Code:
    1. connect(this, SIGNAL(connectSignal(const QString&, int)), &m_ClientSocket, SLOT(connectSlot(const QString&, int)));
    2. connect(this, SIGNAL(writeData(const char*,unsigned int)), &m_ClientSocket, SLOT(writeData(const char*,unsigned int)));
    3. emit(connectSignal("localhost", 1000));
    4. emit(writeData("rtst", 4));
    To copy to clipboard, switch view to plain text mode 
    test() signal still not called. What is wrong?

    connectSlot() and writeData() slots get called.


    Added after 4 minutes:


    if I use Qt::QueuedConnection for 2 last connect()s then neither connectSlot() nor writeData() are called.
    Last edited by R-Type; 28th October 2011 at 09:40.

  4. #24
    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: QTcpSocket: no readyRead() signal even in Qthread event loop

    I see some synchronous things in your code. Besides, if you want to post an example, make it a complete one, including main().
    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. #25
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QTcpSocket: no readyRead() signal even in Qthread event loop

    Quote Originally Posted by wysota View Post
    I see some synchronous things in your code.
    Do you mean connect/write signals sequence? It's just to make example smaller. Normally write should be called in slot for connected() signal, but in example it doesn't matter because local connections are quite fast.
    Quote Originally Posted by wysota View Post
    Besides, if you want to post an example
    As I mentioned, the problem is that in example it works and in real project the same code doesn't call test() slot. Here is the complete example:
    test.h
    Qt Code:
    1. #include <ClientSocket.h>
    2.  
    3. class Test : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. signals:
    8. void connectSignal(const QString& host, int port);
    9. void writeData(const char* data,unsigned int length);
    10.  
    11. public:
    12. Test();
    13. private:
    14. ClientSocket m_ClientSocket;
    15. };
    To copy to clipboard, switch view to plain text mode 
    test.cpp
    Qt Code:
    1. #include "test.h"
    2.  
    3. Test::Test() : QObject(), m_ClientSocket() {
    4. connect(this, SIGNAL(connectSignal(const QString&, int)), &m_ClientSocket, SLOT(connectSlot(const QString&, int)));
    5. connect(this, SIGNAL(writeData(const char*,unsigned int)), &m_ClientSocket, SLOT(writeData(const char*,unsigned int)));
    6. emit(connectSignal("localhost", 1000));
    7. emit(writeData("rtst", 4));
    8. }
    To copy to clipboard, switch view to plain text mode 
    ClientSocket.h
    Qt Code:
    1. #include <QTcpSocket>
    2.  
    3. class ClientSocket : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. ClientSocket() : QObject()
    9. , m_socket(new QTcpSocket(this)) {
    10. connect(m_socket, SIGNAL(readyRead()), this, SLOT(test()));
    11. }
    12. bool connectToHost(QString host, int port) {
    13. m_socket->connectToHost(host, port);
    14. return m_socket->waitForConnected(1000);
    15. }
    16. void write(const char* Data,unsigned int nBytes) {
    17. if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState))
    18. {
    19. m_socket->write(Data);
    20. m_socket->flush();
    21. }
    22. }
    23.  
    24. public slots:
    25. void writeData(const char* Data,unsigned int nBytes) { write(Data, nBytes); }
    26. void connectSlot(const QString& host, int port) { connectToHost(host, port); }
    27. void test() { qDebug()<<"ok"; }
    28.  
    29. private:
    30. QTcpSocket* m_socket;
    31. };
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "test.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. Test t;
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #26
    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: QTcpSocket: no readyRead() signal even in Qthread event loop

    Quote Originally Posted by R-Type View Post
    Do you mean connect/write signals sequence?
    I mean waitFor*() calls and using sleep().

    As I mentioned, the problem is that in example it works and in real project the same code doesn't call test() slot.
    There is no point in fixing code that works Your "real project" apparently does something which breaks the code. It may block the event loop or don't start one at all.
    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. The following user says thank you to wysota for this useful post:

    R-Type (15th December 2011)

Similar Threads

  1. Replies: 1
    Last Post: 22nd July 2010, 09:16
  2. Terminate a QThread with an event loop
    By paolom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 11:53
  3. QThread event loop blocking the GUI
    By JoeMerchant in forum Qt Programming
    Replies: 4
    Last Post: 18th July 2009, 07:54
  4. QThread event loop seems blocked
    By eurodatar in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 16:50
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

Tags for this Thread

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.