Search code examples
c++qtros

How would you implement a ROS publisher and subscriber in an existing QT Creator GUI project?


I currently have a piece of simulator GUI written in QT Creator that simulates a robot's movements, that I would like to integrate with ROS such that it can actually control a robot.

How can a ROS publisher and subscriber node be written into the QT Creator project?

Thanks in advance!


Solution

  • Well, you can take a look at the qt_ros package.

    Basically, the logic will be having a QThreaded node (private member of the Gui class) running behind the gui. It will be responsible for handling all the ROS logic (subscribe, publish, service server/Client...) and then relaying that data to the parent Qt Gui using appropriate methods.

    For inetgrating Qt in a ROS Project, the easiest way would be depending on the qt-build package and in your CMakelists, here is a short setup for making a node with a Gui :

    rosbuild_prepare_qt4(QtCore QtGui QtXml) # Add the appropriate components to the component list here
    
    # Sections
    
    file(GLOB QT_FORMS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/*.ui)
    file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.qrc)
    file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}     FOLLOW_SYMLINKS include/*.h include/*.hpp include/msg-pack-include/*.h)
    
    
    QT4_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
    QT4_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
    QT4_WRAP_CPP(QT_MOC_HPP ${QT_MOC})
    
    # Sources
    
    file(GLOB_RECURSE QT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)
    
    # Binaries
    
    add_executable(qt_gui_node ${QT_MOC_HPP} ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_FORMS_HPP})
    target_link_libraries(qt_gui_node ${QT_LIBRARIES} ${catkin_LIBRARIES})
    

    If you want a cleaner way, you can just copy/paste the qt-ros.cmake.in in a folder named cmake in your project and then include it in your CMakeLists as following :

    include(cmake/qt-ros.cmake.in)
    

    Hope that helps, a startup point at least !

    Cheers,