Results 1 to 20 of 26

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #13
    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

    In #3 and #6 I already wrote that I've envolved QThread to get event loop, because it didn't work as I had Client Socket inherited from QObject. This was just suggestion that it doesn't work because of missing event loop. By the way, socket belongs to thread as it's instantiated in run() (see code above). As I wrote before, this all works also if I use this ClientSocket in my simple appplication from main(). But in Dll of project (plugin) I'm now working with, I have a problem that non blocking methods didn't get called whereas blocking methods work.
    To avoid further confusions with threads I post version without QThread that behaves the same way.
    Qt Code:
    1. class ClientSocket : public QObject {
    2. Q_OBJECT
    3. public:
    4. ClientSocket() : QObject()
    5. , 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. m_socket->waitForBytesWritten();
    19. //bool hasData=m_socket->waitForReadyRead();
    20. //int num=m_socket->bytesAvailable();
    21. //QByteArray data=m_socket->readAll();
    22. //qDebug()<<num<<data;
    23. }
    24. }
    25. public slots:
    26. void test() { qDebug()<<"readyRead()"; }
    27. private:
    28. QTcpSocket* m_socket;
    29. };
    To copy to clipboard, switch view to plain text mode 
    From DLL code it's used like this
    Qt Code:
    1. SomeClass::SomeClass() : m_ClientSocket() {
    2. bool bConnected = m_ClientSocket.connectToHost("localhost", 1000);
    3. if (bConnected) m_ClientSocket.write("rtst", 4);
    4. }
    To copy to clipboard, switch view to plain text mode 
    test() slot is not called. if I uncomment lines in write() I have expected response (num=4, data="rtst") and slot is called. But waitForReadyRead() shouldn't be called in order to get slot working...
    Last edited by R-Type; 25th October 2011 at 09:11.

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
  •  
Qt is a trademark of The Qt Company.