Results 1 to 2 of 2

Thread: Thread(s) and socket, timer slots

  1. #1
    Join Date
    Nov 2006
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Thread(s) and socket, timer slots

    Hi all

    I wrote routines in a form using a QTCpSocket and a QTimer
    to receive a non stop data stream from a server and write data from buffer in a different file each minute. It worked fine in the dialog form.

    So i tried to write a thread class involving a QSocket and a QTimer too.
    I would like to run multiple threads without freezing the user interface.
    My thread runs well but events are never fired...
    The code compiles well, but returns the following messages on execution :

    QObject: Cannot create children for a parent that is in a different thread.
    Object::connect: No such slot ThreadReception::readStream()
    Object::connect: No such slot ThreadReception::displayError(QAbstractSocket::Soc ketError)
    QObject: Cannot create children for a parent that is in a different thread.
    Object::connect: No such slot ThreadReception::topMinute()
    ecc.
    Qt Code:
    1. class ThreadReception : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. ThreadReception(QObject *parent = 0);
    8. ~ThreadReception();
    9.  
    10. bool connectSource();
    11. void unconnectSource();
    12.  
    13. QMutex threadMutex;
    14.  
    15. QTcpSocket *tcpSocket;
    16. QTimer *timerHorloge;
    17.  
    18. (...)
    19.  
    20. private:
    21. void readStream();
    22. void topMinute();
    23. void saveMinuteBuffer();
    24.  
    25. protected:
    26. void run();
    27. void attente();
    28.  
    29. };
    30.  
    31.  
    32. void ThreadReception::run()
    33. {
    34. tcpSocket = new QTcpSocket(this);
    35. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readStream()), Qt::DirectConnection);
    36. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)), Qt::DirectConnection);
    37.  
    38. //initialise un timer
    39. timerClock = new QTimer(this);
    40. connect(timerClock, SIGNAL(timeout()), this, SLOT(topSeconde()), Qt::DirectConnection);
    41. timerHorloge->start(1000); //1 seconde
    42.  
    43. exec();
    44.  
    45. }
    To copy to clipboard, switch view to plain text mode 

    i modified my code as below. first messages relative to the "parent thread" have disappeared but now i get the following messages

    QObject::connect: Cannot connect QTcpSocket::readyRead() to (null)::readStream()
    QObject::connect: Cannot connect QTcpSocket::error(QAbstractSocket::SocketError) to (null)::displayError(QAbstractSocket::SocketError)
    QObject::connect: Cannot connect QTimer::timeout() to (null)::topMinute()
    Qt Code:
    1. void ThreadReception::run()
    2. {
    3. tcpSocket = new QTcpSocket(0);
    4. connect(tcpSocket, SIGNAL(readyRead()), 0, SLOT(readStream()), Qt::DirectConnection);
    5. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), 0, SLOT(displayError(QAbstractSocket::SocketError)), Qt::DirectConnection);
    6.  
    7. //initialise un timer
    8. timerClock = new QTimer(0);
    9. connect(timerClock, SIGNAL(timeout()), 0, SLOT(topSeconde()), Qt::DirectConnection);
    10. timerHorloge->start(1000); //1 seconde
    11.  
    12. exec();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    What is wrong ? Has someone any idea ?

    Best regards

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread(s) and socket, timer slots

    Quote Originally Posted by stephdev1965 View Post
    What is wrong ? Has someone any idea ?
    The problem is exactly what the error states. "Cannot connect something to null". You are passing "0" as receiver in the connect statements. Here's a thread worth reading.
    J-P Nurmi

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.