Callback when user presses radio button in my rqt plugin?

asked 2017-03-19 22:52:25 -0500

danyloM gravatar image

I am writing a C++ rqt plugin which has the following layout:

image description

I would like to know how one would implement a callback function which executes when the user clicks one of the radio buttons? I.e. I want:

  • callback function startEvent to be executed when the user clicks the "Start experiment" radio button
  • callback function stopEvent to be executed when the user clicks the "STOP experiment" radio button

What follows below is the current state of my code, if you need that info. Otherwise you can ignore it. Thanks for helping!

Project tree:

├── CMakeLists.txt
├── include
│   └── rqt_example_cpp
│       └── my_plugin.h
├── package.xml
├── plugin.xml
└── src
    └── rqt_example_cpp
        ├── my_plugin.cpp
        └── my_plugin.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <widget class="QWidget" name="MyPluginWidget">
  <property name="geometry">
  <property name="windowTitle">
  <widget class="QRadioButton" name="radioButton">
   <property name="geometry">
   <property name="text">
    <string>Start experiment</string>
  <widget class="QRadioButton" name="radioButton_2">
   <property name="geometry">
   <property name="font">
   <property name="text">
    <string>STOP experiment</string>



#include <ros/ros.h>
#include <rqt_gui_cpp/plugin.h>
#include <rqt_example_cpp/ui_my_plugin.h>
#include <QWidget>

namespace rqt_plugin {

class MyPlugin : public rqt_gui_cpp::Plugin {



  virtual void initPlugin(qt_gui_cpp::PluginContext& context);
  virtual void shutdownPlugin();
  virtual void saveSettings(qt_gui_cpp::Settings& plugin_settings,
      qt_gui_cpp::Settings& instance_settings) const;
  virtual void restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
      const qt_gui_cpp::Settings& instance_settings);

  // Comment in to signal that the plugin has a way to configure it
  // bool hasConfiguration() const;
  // void triggerConfiguration();


  Ui::MyPluginWidget ui_;
  QWidget* widget_;
  ros::NodeHandle ros_nh_;
  ros::Publisher ros_pub_;


}  // namespace rqt_example_cpp



#include "rqt_example_cpp/my_plugin.h"
#include <pluginlib/class_list_macros.h>
#include <QStringList>

namespace rqt_plugin {

MyPlugin::MyPlugin() : rqt_gui_cpp::Plugin() , widget_(0) {
  // Constructor is called first before initPlugin function, needless to say.

  // give QObjects reasonable names

void MyPlugin::initPlugin(qt_gui_cpp::PluginContext& context)
  // access standalone command line arguments
  QStringList argv = context.argv();
  // create QWidget
  widget_ = new QWidget();
  // extend the widget with all attributes and children from UI file
  // add widget to the user interface

//  getNodeHandle().subscribe();

void MyPlugin::shutdownPlugin()
  // unregister all publishers here

void MyPlugin::saveSettings(qt_gui_cpp::Settings& plugin_settings,
    qt_gui_cpp::Settings& instance_settings) const
  // instance_settings.setValue(k, v)

void MyPlugin::restoreSettings(const qt_gui_cpp::Settings& plugin_settings,
    const qt_gui_cpp::Settings& instance_settings)
  // v = instance_settings.value(k)

/*bool hasConfiguration() const
  return true;

void triggerConfiguration()
  // Usually used to open a dialog to offer the user a set of configuration

}  // namespace rqt_example_cpp
PLUGINLIB_DECLARE_CLASS(rqt_plugin, MyPlugin, rqt_plugin::MyPlugin, rqt_gui_cpp::Plugin)
edit retag flag offensive close merge delete