(!) 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.

Mixing Qt and Boost Signals

Description: Avoiding the keyword issues created by qt's signal/slot macros.

Keywords: qt boost signals

Tutorial Level: BEGINNER

Problem Statement

Qt uses preprocessor macros (fugly) for signals and slots. These conflict with boost's signals and slots class names. End result, is you can't compile when trying to use both in the same library/program.

Qt Workaround

Qt4.1 introduced proper macro namespacing to qt signals and slots to avoid this situation (prefixes with Q_ and converts to uppercase - see below).

Application to Qt Ros

Fuerte++

The tf package will raise this issue as soon as you include a tf header, so since tf is a rather core dependency, qt-ros on fuerte will use these definitions by default. The macro changes you will need to apply in your code are as follows:

  • signals -> Q_SIGNALS

  • slots -> Q_SLOTS

  • emit -> Q_EMIT

  • foreach -> Q_FOREACH

Electric

Electric wasn't updated for this api. To convert to the new keywords, create your new package, e.g.

> rosrun qt_create roscreate-qt-pkg my_package_foo

Suppose you now include the header file tf/message_filter.h to qnode.hpp in one of your sources and also add tf to the manifest.xml:

<depend package="tf" />

Compilation will fail, complaining about signals and slots.

To fix, add a new line in your CmakeList.txt:

rosbuild_prepare_qt4(QtCore QtGui)
ADD_DEFINITIONS(-DQT_NO_KEYWORDS)

Now we change the Qt macros to upper case version. In our example we need to

  • main_window.hpp : change slots -> Q_SLOTS.

  • qnode.hpp : change signals -> Q_SIGNALS.

  • qnode.cpp : change emit to Q_EMIT.

Now everything should compile - be happy!

Thanks to Heber Sobreira for the preceding information.

Wiki: qt_ros/Tutorials/Mixing Qt and Boost Signals (last edited 2012-08-10 15:18:26 by DanielStonier)