ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Why do I get ampersand characters in my strings?

asked 2018-06-18 09:23:22 -0600

VictorLamoine gravatar image

updated 2018-06-18 09:24:53 -0600

I'm trying to fix an issue that appeared on ROS Melodic, I cannot reproduce this on ROS Kinetic.

In short

  • I'm using QCheckBox objects and setting the text to a ROS topic name.
  • I then retrieve this topic name (from the QCheckBox) but ampersand characters randomly appear in the strings.


Use the ampersand branch to reproduce this bug:

  • Compile / launch with command line (roslaunch rqt_bag_exporter gui.launch)
  • Open this bag file
  • Tick /topic/dog topic to be exported
  • Export

On my machine it fails with: image description

The service fails because the topic name has an extra ampersand (&) character after the first slash (/), I cannot find where this character comes from.

More info

It is in the fillTopicsToExport member that the ampersand first appear, before that all ROS_INFO output display the correct topic name.

[ WARN] [1529331726.714089119]: Service server listTopics:
[ INFO] [1529331726.714882690]: /rosout
[ INFO] [1529331726.714903216]: /rosout_agg
[ INFO] [1529331726.714919695]: /topic/dog
[ WARN] [1529331726.715075880]: Topic list retrieved from the service server: 
[ INFO] [1529331726.716353318]: /rosout
[ INFO] [1529331726.716371703]: /rosout_agg
[ INFO] [1529331726.716381502]: /topic/dog
[ WARN] [1529331726.716421088]: Display topics
[ INFO] [1529331726.716500749]: /rosout
[ INFO] [1529331726.716593479]: /rosout_agg
[ INFO] [1529331726.716648656]: /topic/dog
[ WARN] [1529331732.194335365]: fillTopicsToExport
[ INFO] [1529331732.194412935]: /&topic/dog


  • Kubuntu 18.04
  • ROS Melodic (up to date as of today)
  • Qt 5.9.5
  • g++ 7.3.0

I have failed to reproduce this behavior in a simple Qt program.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-06-18 09:37:03 -0600

gvdhoorn gravatar image

updated 2018-06-18 09:41:53 -0600

I'm pretty sure you're running into this issue: [PyQt] QPushButton.text() ampersand issue:

I'm having an issue getting the text from a QPushButton.The text of the button is set with:

statusButton = QPushButton(translator.translate('rb', "Start"), self)

the text appears normally on the button (i.e. without an ampersand), but when I get the text with:

status = statusButton.text()

the string returned is "&Start" instead of "Start".

The most likely reason is that Qt automatically makes the first (non-special) character of a string set as the label in a QCheckBox the shortcut character (docs for QCheckBox, search for "shortcut").

If you then ask for the label using .text(), the return value is the text with the &, as that is how you would normally indicate which character should be the one registered as the shortcut for that UI element.

Edit: to avoid this the nicest approach would probably be to go with an MVC implementation, where the model is a list of strings and the view creates a QCheckBox instance for each entity in the model. You would then interact with the model instead of with the QCheckBox labels directly.

edit flag offensive delete link more


Note: the post I linked to refers to PyQt, but the issue would be the same for C++.

gvdhoorn gravatar image gvdhoorn  ( 2018-06-18 09:37:26 -0600 )edit

I finally have a clue of what is happening :) I still don't understand when this renaming happens: just after setting the text, retrieving the text is ok, if retrieving in a SLOT then the text is modified. I'll use setObjectName instead of the text as this is meant for this kind of usage.

VictorLamoine gravatar image VictorLamoine  ( 2018-06-18 09:45:35 -0600 )edit

Question Tools

1 follower


Asked: 2018-06-18 09:23:22 -0600

Seen: 787 times

Last updated: Jun 18 '18