Connecting to a slot in a QThread
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?
I think the error here is bigger than you realise.
QThreadis not the thread, it's a wrapper around it.
QThreadwill actually live in the main thread so any slot will be executed by the main thread and that is bad.
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!
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?
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
QThreadsubclass the slots will be executed in the thread that created the
ros_comm, not in the thread created by