Results 1 to 9 of 9

Thread: signal/slot accross different threads(different subclasses QThread)

  1. #1
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15

    Default signal/slot accross different threads(different subclasses QThread)

    I want to emit a signal from a thread and the slot to be executed on another thread(of different subclass QThread).
    For example,
    i have these subclasses QThread: ServerThread and ClientThread.
    I want to emit signal from ClientThread and in ServerThread class a slot to be executed.
    The problem is,when multiple ClientThreads* threads emit signals then slots from ServerThreads* threads are executed only for the first signal that emitted.
    Why this occurs?
    If am not clear,tell me to post sample code.
    Thanks!
    Last edited by milli; 24th June 2011 at 20:58.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: signal/slot accross different threads(different subclasses QThread)

    Yes, post your code, it'll be easier to spot the problem, now we can only guess.

  3. #3
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15

    Default Re: signal/slot accross different threads(different subclasses QThread)

    Qt Code:
    1. void MainServer::newConnectionFromAdmin() {
    2.  
    3.  
    4. QTcpSocket * tcpSocket = tcpServer.nextPendingConnection();
    5. ServerThread *serverThread = new ServerThread(tcpSocket->socketDescriptor(),this);
    6. serverThread->start();
    7. connect(serverThread,SIGNAL(finished()),serverThread,SLOT(deleteLater()));
    8. }
    9.  
    10.  
    11.  
    12. void MainServer::newConnectionFromClient()
    13. {
    14.  
    15. QTcpSocket * tcpSocket = tspServer.nextPendingConnection();
    16. ClientThread *clientThread = new ClientThread(tcpSocket->socketDescriptor(),this);
    17. clientThread->start();
    18. connect(clientThread,SIGNAL(finished()),clientThread,SLOT(deleteLater()));
    19. }
    20.  
    21. ServerThread::ServerThread(int num, QObject *parent)
    22. : QThread(parent), num(num)
    23. {
    24.  
    25. tcpSocket=new QTcpSocket(this);
    26. tcpSocket->setSocketDescriptor(num);
    27.  
    28.  
    29. //connections for sockets
    30. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readReq()));
    31. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    32.  
    33.  
    34.  
    35.  
    36. }
    37. /* The running thread's running code */
    38. void ManagerServerThread::run()
    39. {
    40. connect(MainServer *)(this->parent()),SIGNAL(reportToServer(TRep*)),this,SLOT(writeData(TRep*)));
    41. exec();
    42. }
    43.  
    44.  
    45. ClientThread::ClientThread(int num, QObject *parent)
    46. : QThread(parent), num(num)
    47. {
    48.  
    49. tcpSocket.setSocketDescriptor(num);
    50.  
    51.  
    52. //connections for sockets
    53. connect(&tcpSocket, SIGNAL(readyRead()), this, SLOT(func()));
    54. connect(&tcpSocket, SIGNAL(disconnected()), this, SLOT(connectionClosedByServer()));
    55. connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    56.  
    57.  
    58. connect(this,SIGNAL(reportToAdmin(TRep*)),(MainServer *)(this->parent()),SIGNAL(reportToServer(TRep*)));
    59.  
    60. // qDebug() << Q_FUNC_INFO << QThread::currentThreadId();
    61.  
    62. }
    63. void ClientThread::run()
    64. {
    65. exec();
    66. }
    67. int ClientThread::func()
    68. {
    69. emit reportToAdmin(rep);
    70. {
    To copy to clipboard, switch view to plain text mode 
    Last edited by milli; 24th June 2011 at 23:02.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: signal/slot accross different threads(different subclasses QThread)

    Short description of the code could be good to - what results do you expect, and what results you get ?

  5. #5
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15

    Default Re: signal/slot accross different threads(different subclasses QThread)

    So,when the signal reportToAdmin(rep) is emitted i want slot writedata (ServerThread class) to be executed.When one thread is running the the result is expecting.However when multiple threads emit signal reportToAdmin(rep) then slots from ServerThreads* threads are executed only for the first signal that is emitted.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: signal/slot accross different threads(different subclasses QThread)

    I guess you should be getting some warnings, saying connection not made or object not found... check the Application Output

    One quick comment, you are creating ClientThread in the thread context where MainServer resides, and trying to emit a signal (which is of ClientThread's object) from some other thread, you need to move the ClientThread object to the context of thread which emit's reportToAdmin().

    one quick try, if this is the only problem is to add moveToThread(clientThread); as last thing in void MainServer::newConnectionFromClient()
    Last edited by Santosh Reddy; 25th June 2011 at 05:09.

  7. #7
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    15

    Default Re: signal/slot accross different threads(different subclasses QThread)

    No i am not getting errors.I added moveToThread(clientThread) but the result is the same..Could someone to propose me a good tutorial with examples for QThreads except Qt Documentation which i have read it.
    Thanks!

  8. #8
    Join Date
    Apr 2011
    Posts
    124
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60
    Thanks
    1
    Thanked 10 Times in 10 Posts

    Default Re: signal/slot accross different threads(different subclasses QThread)

    My guess is that reportToAdmin isn't returning, but is waiting somewhere. A new signal won't be delivered until the receiving thread returns to its event loop.

  9. The following user says thank you to DanH for this useful post:

    milli (25th June 2011)

  10. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: signal/slot accross different threads(different subclasses QThread)

    Quote Originally Posted by milli
    Could someone to propose me a good tutorial with examples for QThreads except Qt Documentation which i have read it.
    Reading the documentation is not enough, you need to really understand the how QThread works, and to do so you need have a understand threads and multi-threading concepts (in addition to C, C++, Qt)

    Please consider this reply as an encouragement to understand the things better, rather than just knowing / using them.

  11. The following user says thank you to Santosh Reddy for this useful post:

    milli (25th June 2011)

Similar Threads

  1. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06
  2. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  3. QTcpServer and QThread signal/slot problem
    By Diph in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 19:34
  4. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 5th November 2007, 23:56
  5. Signal / Slot with specific argument and threads
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 14th September 2007, 23:43

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.