Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to get readyRead signal for tcp socket on different thread SLOT
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aashish.lg
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aashish.lg
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aashish.lg
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

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

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved