[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.



  • 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/Threads_Events_QObjects wiki article for details of the now recommended way.



  • I have got this code snippet from the above mentioned link.
    @class Worker : public QObject
    {
    Q_OBJECT

    public slots:
    void doWork() {
    /* ... */
    }
    };

    /* ... */
    QThread *thread = new QThread;
    Worker *worker = new Worker;
    connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
    worker->moveToThread(thread);
    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



  • 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.



  • [quote author="Ashish Mittal" date="1329131286"]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.
    [/quote]

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.