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. QThread movetoThread howto?
Qt 6.11 is out! See what's new in the release blog

QThread movetoThread howto?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views
  • 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.
  • R Offline
    R Offline
    Reudi
    wrote on last edited by VRonin
    #1

    Dear People of the Internet, I'm realy confused about the usage of QThread.
    I got a little class which communicates over a SerialPort with a little robot.
    So far so good. Sadly this class needs to wait for the robot to finish with the execution of his task.

    So i got a small "commincation class"

    //conenction Object
    class SerialConnection: public QObject
    {
        Q_OBJECT
        public:
            explicit SerialConnection( QObject *parent = 0);
            virtual ~SerialConnection();
    
           //evil Method which needs alot of time to executed and needs to be threaded
            int move(QPoint);
    
         //other evil Method needs alot of time
         int concurWorld(bool showMercy);
    }
    

    I got a main class which calls the "move" and concurWorld methods. Those Methods wont be called at the same time.
    All i want to do is to Call those methods in a Thread so it doesnt block my main appplication.
    Somehow it should work like this

    void mainClass::callmove()
    {
        QThread * thread = new QThread();
        SerialConnection  * serial  = new SerialConnection();
       serial->moveToThread(thread);
     //at this point I got no clue how to continue
    
       
    }
    

    I know that i need to change the functions to Slots in my serialconnection class and i know that i can connect the start signal of the thread to one of those slots.
    But how can i parse some arguments to those methods and how can i get the return values? And how can i connect both of them to a thread when there is only the start singnal of thread?
    I feel so dump for asking such a question but the Internet itself seems to be split on deciding which is the right way to use QThreads.

    Thanks for the help

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      class SerialConnection: public QObject
      {
          Q_OBJECT
          public:
              explicit SerialConnection( QObject *parent = 0);
              virtual ~SerialConnection();
      signals:
      void moveReturned(int);
      void concurWorldReturned(int);
      public slots:
      void runMove(QPoint val){emit moveReturned(move(val));}
      void runConcurWorld(bool val){emit concurWorldReturned(concurWorld(val));}
      private:
             //evil Method which needs alot of time to executed and needs to be threaded
              int move(QPoint);
      
           //other evil Method needs alot of time
           int concurWorld(bool showMercy);
      }
      
      void mainClass::callmove()
      {
          QThread * thread = new QThread();
          SerialConnection  * serial  = new SerialConnection();
         serial->moveToThread(thread);
      connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
      connect(thread, SIGNAL(finished()), serial, SLOT(deleteLater()));
      thread->start();
      
         
      }
      

      now you can use any signal connected to the 2 new slots to run it (or use QMetaObject::invokeMethod with Qt::QueuedConnection) and when the process is finished the object will emit the signal with the return value

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • R Offline
        R Offline
        Reudi
        wrote on last edited by
        #3

        Thank you kind Sir! I'll always be in your debt!
        Cheers.

        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