Results 1 to 5 of 5

Thread: How to use QThread::exec()

  1. #1
    Join Date
    Feb 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy How to use QThread::exec()

    Does anyone have any experience with this?

    The QT 4.1 examples do not make use of this approach, but the Qthread class documentation shows pseudo code that does. I am drawn to this approach because I normally have my client in request/reply mode with a server, but sometimes the server sends data asynchronously on the socket.

    Specifically I'd like to see an eaxmple that has the main run() function use QThread:exec() and have member functions for other threads to call to post requests to the server and have the thread receive messages from the server (QTCPSocket signals) , read tyhem correctly, parse them and emit signals for all server messages to anyone that might be listening.

    As a side note, how about protecting the socket read and write functions separately as they are really two distinct pipes.

    thanks in advance!

  2. #2
    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: How to use QThread::exec()

    But what exactly is your question? QThread::exec() works the same as QCoreApplication::exec(), it starts an event queue processing.

  3. #3
    Join Date
    Feb 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QThread::exec()

    My question is how to have a QTcpSocket in the thread, use the socket to send requests to a server via methods callable from the GUI thread, have the thread listen for server messages and then create signals the GUI thread can be connected to obtain the server messages.

  4. #4
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: How to use QThread::exec()


  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: How to use QThread::exec()

    You don't need a separate thread for that... you can do this all in one thread. If you want to "call" methods of a separate thread from the "main" thread, it involves synchronisation between those two. You can use signals & slots for that and then it is as easy as connecting them and emitting a signal. But remember that it is all an event driven mechanism and two threads don't wait for each other here...

    Some small partial example (this is not a Newbie section, so I expect you can fill the rest).
    Qt Code:
    1. class MyThread : public QThread{
    2. public:
    3. void run(){
    4. QTcpSocket *sock = new QTcpSocket;
    5. connect(this, SIGNAL(sendToSocket(...)), sock, SLOT(...)));
    6. // ...
    7. exec();
    8. }
    9. // ...
    10. };
    11.  
    12. MyThread *thr1;
    13. //...
    14. connect(thr1, SIGNAL(messageReceived(...)), this, SLOT(onMessageReceived(...)));
    15. connect(this, SIGNAL(sendToThread(...)), thr1, SIGNAL(SendToSocket(...)));
    16. thr1->start();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Calling QThread::exec() repeatedly
    By hb in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2007, 20:24

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.