Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Connecting to a slot in a QThread
Forum Updated to NodeBB v4.3 + New Features

Connecting to a slot in a QThread

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 4.2k 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.
  • C Offline
    C Offline
    Christianvs
    wrote on last edited by
    #1

    Hi

    I got a problem connecting a signal from an object in my MainWindow to a QThread object that is also defined in my MainWindow.

    In my MainWindow i create an object of type QThread called ros_comm_ which is of the class ros_comm.
    Besides that i create an object called userTemp of class user. userTemp emits following signals

    signals:
        void ctrlParamsChanged(rtl_msgs::control_parameters ctrlParams);
        void targetForceChangedSignal(rtl_msgs::target rtlt);
    

    I am trying to connect the signals to two public slots in the ros_comm_ object

    public slots:
        void publish_target_values(rtl_msgs::target &target_msg);
        void call_srv_control_parameters(rtl_msgs::control_parameters &cp_msgs);
    

    Since ros_comm_ is a QThread i have had some problems connecting the signals with the slots. I found this one guide where i added following macros to my MainWindow header file:

    Q_DECLARE_METATYPE(rtl_msgs::target target_msg)
    Q_DECLARE_METATYPE(rtl_msgs::control_parameters cp_msgs)
    

    Then before connecting my signals/slots i inserted the lines qRegisterMetaType<type>();

    qRegisterMetaType<rtl_msgs::control_parameters>();
    connect(userTemp, SIGNAL(ctrlParamsChanged(rtl_msgs::control_parameters)),
            &ros_comm_, SLOT(call_srv_control_parameters(rtl_msgs::control_parameters)));
    
    qRegisterMetaType<rtl_msgs::target>();
    connect(userTemp, SIGNAL(targetForceChangedSignal(rtl_msgs::target)),
            &ros_comm_, SLOT(publish_target_values(rtl_msgs::target&)));
    

    Whatever the macro and the qRegisterMetaType line does, it doesn't work for me. I've been reading up on QThread documentation, but i haven't found any ways to actually connect the signals/slots.

    Anyone who can help me out?

    Thanks

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I think the error here is bigger than you realise. QThread is not the thread, it's a wrapper around it. QThread will actually live in the main thread so any slot will be executed by the main thread and that is bad.

      Have a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      "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

      C 2 Replies Last reply
      4
      • VRoninV VRonin

        I think the error here is bigger than you realise. QThread is not the thread, it's a wrapper around it. QThread will actually live in the main thread so any slot will be executed by the main thread and that is bad.

        Have a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        C Offline
        C Offline
        Christianvs
        wrote on last edited by
        #3

        @VRonin
        Thanks for the reply!

        The ros_comm class in my program is a (or part of a) bridge between ROS kinetic and QT creator i believe and it is used to communicate with some sensors and actuators. I am not the author to the ros_comm class, the author is one from my university and i think he knew what he did when he wrote it all. I just use his code to build on and i though i'd might be able to make a public slot in the ros_comm class :)

        But thanks for the link, i'll look into it and see if i can find a solution to my problem!

        1 Reply Last reply
        0
        • VRoninV VRonin

          I think the error here is bigger than you realise. QThread is not the thread, it's a wrapper around it. QThread will actually live in the main thread so any slot will be executed by the main thread and that is bad.

          Have a look at https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

          C Offline
          C Offline
          Christianvs
          wrote on last edited by
          #4

          @VRonin

          If i get this right, then it isn't possible to have an object on my main thread while having it connected with slot on both my main thread and the QThread. You have to move the objects thread affinity to the thread the QThread wraps around, which (if i get this right) means that you can't connect to the slots on the main thread.
          Or am i wrong about this?

          VRoninV 1 Reply Last reply
          0
          • C Christianvs

            @VRonin

            If i get this right, then it isn't possible to have an object on my main thread while having it connected with slot on both my main thread and the QThread. You have to move the objects thread affinity to the thread the QThread wraps around, which (if i get this right) means that you can't connect to the slots on the main thread.
            Or am i wrong about this?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @Christianvs said in Connecting to a slot in a QThread:

            Or am i wrong about this?

            You are, sorry. You are free to connect signals and slots in different threads as long as both are running an event loop. Qt will take care of making the connection safe.

            i'd might be able to make a public slot in the ros_comm class

            You can but if ros_comm is a QThread subclass the slots will be executed in the thread that created the ros_comm, not in the thread created by ros_comm

            "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
            • O Offline
              O Offline
              opengpu
              wrote on last edited by
              #6
              This post is deleted!
              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