February 12, 2012

Ashish Mittal Ashish Mittal
Ant Farmer
151 posts

[SOLVED] How to get readyRead signal for tcp socket on different thread SLOT

 

Hi,
I have created a Qt GUI app, from there I have created a separate thread of execution, in the run method of the thread I have created a QTcpSocket and registered readyRead() signal with some SLOT in the same thread class.

My prob is GUI thread is calling this SLOT everytime , I am expecting SLOT should be get called from the worker thread, otherwise there is no use of using the separate thread of execution.

Note :- I have tried to create the socket both the way using heap and stack of the worker thread.

4 replies

February 12, 2012

Volker Volker
Robot Herder
5428 posts

You’re doing it wrong™. Subclassing QThread is no longer the recommended way of implementing multithreading in Qt. Please have a look at the Threads, Events and QObjects wiki article for details of the now recommended way.

February 13, 2012

Ashish Mittal Ashish Mittal
Ant Farmer
151 posts

I have got this code snippet from the above mentioned link.

  1. class Worker : public QObject
  2. {
  3.     Q_OBJECT
  4.  
  5. public slots:
  6.     void doWork() {
  7.         /* ... */
  8.     }
  9. };
  10.  
  11. /* ... */
  12. QThread *thread = new QThread;
  13. Worker *worker = new Worker;
  14. connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
  15. worker->moveToThread(thread);
  16. thread->start();

I don’t understand what the obj is here?
How I will signal to do some job inside Worker class.

My problem scenario is mentioned below:-
I wanted to have one QObject derived class there , I will be having one QWebView. From this class I wanted to create separate thread of execution, and I wanted to create one tcpsocket and it should run in that thread class. I will be registering for readyread() and this signal should be called from that separate thread not from GUI thread.
Additionally , I waned to signal this thread class to write some data on this socket.

please suggest a cleaner approach to do this.
If you have some code snippet for writting worker threads , please share with me.
It is bit urgent

Thanks in Advance

February 13, 2012

Ashish Mittal Ashish Mittal
Ant Farmer
151 posts

Thanks a lot Volker!!!!, I am able to solve the problem by moving my entire code from custom thread class to a class derived from Qobject and I simply called
MoveToThread() on this object.

Now I am getting readyRead() signals using worker thread not by GUI thread.

February 13, 2012

Volker Volker
Robot Herder
5428 posts
Ashish Mittal wrote:
Thanks a lot Volker!!!!, I am able to solve the problem by moving my entire code from custom thread class to a class derived from Qobject and I simply called MoveToThread() on this object.

Correct. That’s the way QThread is supposed to use now.

 
  ‹‹ (seemingly) simple Qt app: creating a progress bar      How to use a special qm file? ››

You must log in to post a reply. Not a member yet? Register here!