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
├── setup.py
└── src
    └── rqt_example_cpp
        ├── my_plugin.cpp
        └── my_plugin.ui

my_plugin.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MyPluginWidget</class>
 <widget class="QWidget" name="MyPluginWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QRadioButton" name="radioButton">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>10</y>
     <width>141</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>Start experiment</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButton_2">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>40</y>
     <width>191</width>
     <height>21</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>STOP experiment</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

my_plugin.h:

#ifndef RQT_EXAMPLE_CPP_MY_PLUGIN_H
#define RQT_EXAMPLE_CPP_MY_PLUGIN_H

#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 {
  Q_OBJECT

public:

  MyPlugin();

  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();

private:

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

};


}  // namespace rqt_example_cpp

#endif  // RQT_EXAMPLE_CPP_MY_PLUGIN_H

my_plugin.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
  setObjectName("MyPlugin");
}

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
  ui_.setupUi(widget_);
  // add widget to the user interface
  context.addWidget(widget_);

//  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