Results 1 to 8 of 8

Thread: Qthread

  1. #1
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Qthread

    Qt Code:
    1. class ClientThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ClientThread(QObject *parent,int descriptor);
    7. ~ClientThread();
    8.  
    9. private:
    10. int descriptor;
    11. void run();
    12.  
    13. private slots:
    14. void onFinish();
    15. void tcpError( QAbstractSocket::SocketError error );
    16. void onStateChanged( QAbstractSocket::SocketState socketState);
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "clientthread.h"
    2.  
    3. ClientThread::ClientThread(QObject *parent, int descriptor)
    4. : QThread(parent)//, socket(this)
    5. {
    6. this->descriptor=descriptor;
    7. qWarning("ClientThread::ClientThread() %d\n", QThread::currentThreadId());
    8.  
    9. connect(this,SIGNAL(finished()),this,SLOT(onFinish()));
    10.  
    11. }
    12.  
    13. ClientThread::~ClientThread()
    14. {
    15.  
    16. }
    17.  
    18. void ClientThread::run(){
    19.  
    20. QTcpSocket socket;
    21.  
    22. if( !socket.setSocketDescriptor( descriptor ) )
    23. {
    24. qDebug( "Socket error!" );
    25. return;
    26. }
    27. connect(&socket,SIGNAL( stateChanged (QAbstractSocket::SocketState)),this,SLOT (onStateChanged(QAbstractSocket::SocketState)));
    28. connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(tcpError(QAbstractSocket::SocketError)) );
    29.  
    30. QString add=socket.peerAddress().toString();
    31. qWarning()<<add;
    32.  
    33. qWarning("ClientThread::run() %d\n", QThread::currentThreadId());
    34. exec();// this will keep the thread running.
    35. }
    36.  
    37. void ClientThread::onFinish(){
    38. int a = 12;
    39.  
    40. }
    41.  
    42.  
    43.  
    44. void ClientThread::tcpError(QAbstractSocket::SocketError error){
    45.  
    46. if( error == QAbstractSocket::RemoteHostClosedError )
    47. return;
    48.  
    49. }
    50.  
    51. void ClientThread::onStateChanged( QAbstractSocket::SocketState socketState){
    52. qWarning("ClientThread::onStateChanged() %d\n", QThread::currentThreadId());
    53. switch (socketState){
    54. case QAbstractSocket::UnconnectedState:
    55. qWarning()<<"UnconnectedState";
    56. default:
    57. qWarning()<<socketState;
    58. }
    59.  
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 


    this two lines are not working :

    connect(&socket,SIGNAL( stateChanged (QAbstractSocket::SocketState)),this,SLOT (onStateChanged(QAbstractSocket::SocketState)));
    connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(tcpError(QAbstractSocket::SocketError)) );

    and show error:

    "QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketError'
    (Make sure 'QAbstractSocket::SocketError' is registered using qRegisterMetaType(
    ).)
    QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
    (Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType(
    ).)"

    can anyone tell me how to link the signals of QtcpSocket to the slot in side of qthread???

    qt is fucked

  2. #2
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qthread

    by the way, there is no compile error. it is real time error, so the slot will never get triggtered.

    does any one know what cause this fail?

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread

    threads use queued connection ... use Q_DECLARE_METATYPE and qRegisterMetaType ..

    with queued connection the parameter must be of types that are known to qt,s meta object system
    because qt needs to copy argument to store them in an event behind scene ...
    register "QAbstractSocket::SocketState"
    Last edited by wagmare; 13th May 2009 at 09:19.

  4. #4
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: Qthread

    Quote Originally Posted by wagmare View Post
    threads use queued connection ... use Q_DECLARE_METATYPE and qRegisterMetaType ..

    with queued connection the parameter must be of types that are known to qt,s meta object system
    because qt needs to copy argument to store them in an event behind scene ...
    register "QAbstractSocket::SocketState"
    but QAbstractSocket::SocketState is know type, if i move this outside the thread, then it works good!!~~~

    gotta be the other problems

  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

    Quote Originally Posted by dognzhe View Post
    gotta be the other problems
    No, wagmare is right - you need to register the type with Qt's meta-data system and with QVariant to be able to use it as an argument for a signal that is emitted across threads.
    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
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread

    Quote Originally Posted by dognzhe View Post
    but QAbstractSocket::SocketState is know type, if i move this outside the thread, then it works good!!~~~
    even for emitting SIGNAL with QString .. i have to register it ....

  7. #7
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qthread

    do you know why emit behave like this in the qthread? in the main very thing is fine!

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread

    Quote Originally Posted by dognzhe View Post
    do you know why emit behave like this in the qthread? in the main very thing is fine!
    i stated it clearly in my first reply ....

    Qt supports three types of signal-slot connections:
    http://doc.trolltech.com/4.2/threads...across-threads

    here we are using queued connection
    * with queued connections, the parameters must be of types that are known to qt's meta object system

    * because Qt needs to copy the arguments to store them in an event behind the scene ..

    if error came
    " QObject ::connect:cannot queue arguments of type 'MyType' "

    call qRegisterMetaType() to register the data type 'MyType' before you establish the connection ....

Similar Threads

  1. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22: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.