Results 1 to 4 of 4

Thread: how to operate tcpsocket in thread again

  1. #1

    Default how to operate tcpsocket in thread again

    I establish a multi-thread server,but encouter some problems,after declare tcpsocket in thread::run,I want to operate the tcpsocket in slot ,such as write or read,but i couldnot.
    how to resolve this problem,forgive my poor english,3x

    the thread.h as follows
    Qt Code:
    1. class FortuneThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. FortuneThread(int socketDescriptor, QObject *parent);
    7.  
    8. void run();
    9.  
    10. signals:
    11. void error(QTcpSocket::SocketError socketError);
    12. void toserver(const QString info);
    13.  
    14. private:
    15. int socketDescriptor;
    16. QTcpSocket *tcpSocket;
    17. bool stopped;
    18. private slots:
    19. void recfserver(const QString peeradd);
    20. };
    To copy to clipboard, switch view to plain text mode 

    the thread.cpp as follows
    Qt Code:
    1. FortuneThread::FortuneThread(int socketDescriptor, const QString &fortune,
    2. QObject *parent)
    3. : QThread(parent), socketDescriptor(socketDescriptor)
    4. {
    5.  
    6. };
    7.  
    8. void FortuneThread::run()
    9. {
    10. tcpSocket = new QTcpSocket();
    11.  
    12. if (!((*tcpSocket).setSocketDescriptor(socketDescriptor))) {
    13. emit error((*tcpSocket).error());
    14. return;
    15. }
    16.  
    17. (*tcpSocket).waitForDisconnected();
    18.  
    19. }
    20.  
    21. void FortuneThread::recfserver(const QString peeradd)
    22. {
    23.  
    24. QString text=tr("transfer success!");
    25. QByteArray block;
    26. QDataStream out(&block, QIODevice::WriteOnly);
    27. out.setVersion(QDataStream::Qt_4_0);
    28.  
    29. out << (quint16)0;
    30. out << text;
    31.  
    32. (*tcpSocket).write(block);
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

    when i debug,the error as follows

    QObject cannot create children for a parent that is in a different thread.
    (parent is QNativeSocketEngine,parent's thread is FortuneThread,current thread
    is QThread)
    Last edited by jacek; 26th November 2008 at 17:23. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to operate tcpsocket in thread again

    i) use [code] tags
    ii) with Qt signals & slots and you do not really need to put that code into a separate thread
    iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
    (The QThread belongs to the thread that created it, not to the thread it is executing itself.)

    HTH

  3. #3

    Default Re: how to operate tcpsocket in thread again

    Quote Originally Posted by caduel View Post
    i) use [code] tags
    ii) with Qt signals & slots and you do not really need to put that code into a separate thread
    iii) if using QThread: put your code into a separate class (not the QThread subclass!) and create that in your thread's run() function. You should not add slots/signals to the QThread subclass itself. There are some postings that elaborate on that. Search for them, please.
    (The QThread belongs to the thread that created it, not to the thread it is executing itself.)

    HTH
    thanks for your help,but i think i don't explain my desigh explicility

    firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
    secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
    now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
    could you tell me what other methods which can realize the function
    thank you very much!

  4. #4

    Default Re: how to operate tcpsocket in thread again

    Quote Originally Posted by succulent_lily View Post
    thanks for your help,but i think i don't explain my desigh explicility

    firstly,I want to create a thread,and creat a socket in the thread for some client's connection.
    secondly,I want the gui-mainthread to notify the created thread to send some message to the client..So I use signal/slot for it,just like i clicked a button,then emit a signal which send by gui to the created thread,it is a blocked operation.
    now the slot recfserveris useful for accept the signal,then i want to operate tcpsocket in the slot.can i into the run for the operation?
    could you tell me what other methods which can realize the function
    thank you very much!
    i subclass Qtcpsocket,and transfer the signal to the thread ,then to the socket,the problem is solved ,thanks for your help

Similar Threads

  1. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  2. Replies: 5
    Last Post: 17th January 2008, 21:49
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

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.