(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

To show error or exception message to users

Description: Shows error/warning msgs or progress bar on GUI by using a component in rqt_py_common pkg

Tutorial Level: INTERMEDIATE

Motivation

GUIs should better notify errors and progress to users to some extent, and GUI developers should NOT expect users to pay attention to terminals where they can easily print error messages to. Therefore it is recommended for all rqt plugins to indicate those on GUI.

This tutorial shows a way to do it by using a component in rqt_py_common pkg. Here we look at rqt_topic v 0.2.16 plugin as an example.

Steps to implement

Instantiate `PluginContainerWidget`

(its API doc), taking the plugin's main widget (Full code is available).

   1 class Topic(Plugin):
   2     def __init__(self, context):
   3         :
   4         self._widget = TopicWidget(self)
   5         :
   6         self.mainwidget = PluginContainerWidget(self._widget, True, False)

Looking at its constructor:

   6         self.mainwidget = PluginContainerWidget(self._widget, True, False)

Both 2nd and 3rd arguments are boolean. 3rd turns on message pane, 4th is for progress bar.

/!\ progress bar isn't implemented as of March 2013.

Create `Signal`s in plugin's widget class

/!\ From here only message pane will be discussed. That of progress bar that isn't implemented as of March 2013 will be added once it's implemented. Sorry for the inconvenience.

The widget class is the one that's passed to PluginContainerWidget.

In TopicWidget (Full code),

   1 from python_qt_binding.QtCore import Qt, QTimer, Signal, Slot
   2 :
   3 class TopicWidget(QWidget):
   4     :
   5     sig_sysmsg = Signal(str)
   6 
   7     def __init__(self, plugin=None, selected_topics=None,
   8                  select_topic_type=SELECT_BY_NAME):
   9     :
  10     def _kick_refresh_topics(self):
  11         """
  12         Calling internally self.refresh_topics method.
  13         Reason of existence is to catch possible exception raised from
  14         refresh_topics method that can be used as a callback.
  15         """
  16         try:
  17             self.refresh_topics()
  18         except Exception as e:
  19             self.sig_sysmsg.emit(e.message)

What needs to be added:

   3 class TopicWidget(QWidget):

Note that under current implementation of PluginContainerWidget, the name of the Signal variable MUST BE sig_sysmsg.

Emit signal wherever necessary

  16         try:
  17             self.refresh_topics()
  18         except Exception as e:
  19             self.sig_sysmsg.emit(e.message)

The string that's passed to emit will be shown in message pane.

Wiki: rqt/Tutorials/To show error or exception message to the users (last edited 2013-04-17 19:46:45 by IsaacSaito)