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

ROS Qt creator GUI

asked 2012-01-27 07:43:00 -0500

tairen gravatar image

updated 2014-01-28 17:11:12 -0500

ngrennan gravatar image

Hi, everyone, I am a freshman on ROS and Qt. Sorry to post long question in next, I am overwhelming by incorporate Qt GUI in ROS for almost 4 days, I follow the former posts, post1, and post2 and their related links, and thanks for their answers, I could use Qt creator to compile the ROS packages. However, when I try to bind the Qt program to ROS program, I do not find way to do, (mainly on the CMakeList.txt ). So next is my question: I want to build a simple interface to play with the turtle in learning_joy package instead of using joystick.

  1. According to the tutorials on joystick, I could control the turtle with my joystick. So I use the CMakeList.txt as the copy to add some Qt creator CMake inside. The original learing_joy CMakeList.txt is

        cmake_minimum_required(VERSION 2.4.6)
        include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
    
    
    #set(ROS_BUILD_TYPE RelWithDebInfo)
    
    rosbuild_init()
    
     #set the default path for built executables to the "bin" directory
     set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
     #set the default path for built libraries to the "lib" directory
     set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
    
     # Set the example from the Qt
    
     #uncomment if you have defined messages
     rosbuild_genmsg()
     #uncomment if you have defined services
     #rosbuild_gensrv()
    
     #common commands for building c++ executables and libraries
     #rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
     #target_link_libraries(${PROJECT_NAME} another_library)
     #rosbuild_add_boost_directories()
     #rosbuild_link_boost(${PROJECT_NAME} thread)
     #rosbuild_add_executable(example examples/example.cpp)
     rosbuild_add_executable(turtle_teleop_joy  src/teleop_turtle_joy.cpp)
     #target_link_libraries(example ${PROJECT_NAME})
    
  2. By searching CMake and Qt example, I find one example "HelloworldQt", which includes 5 files: HelloWidget.cpp, HelloWidget.h, Mainwindow.cpp, MainWindow.h, and main.cpp, I also ran this Qt project on my Qt creator,

    it's CMakeList.txt is"

        CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
         PROJECT(HelloWorldQt)
    
    
    SET(CMAKE_BUILD_TYPE Release)
    SET(CMAKE_CXX_FLAGS "-Wall")
    
    # QT4 Handling
    FIND_PACKAGE(Qt4 REQUIRED)
    
    INCLUDE(${QT_USE_FILE})
    
     SET( HWQ_Qt4_SRC
             src/MainWindow.h
             src/HelloWidget.h
       )
     SET( HWQ_Qt4_UI
        )
     SET( HWQ_Qt4_RES
         )
    
     QT4_WRAP_CPP(HWQ_MOC_CPP ${HWQ_Qt4_SRC})
     QT4_WRAP_UI(HWQ_UI_CPP ${HWQ_Qt4_UI})
     QT4_ADD_RESOURCES(HWQ_RES_H ${HWQ_Qt4_RES})
    
     INCLUDE_DIRECTORIES( . )
    
     # General
     SET( HWQ_SRC
          src/main.cpp
          src/MainWindow.cpp
          src/HelloWidget.cpp
          ${HWQ_MOC_CPP}
          ${HWQ_UI_CPP}
          ${HWQ_RES_H}
        )
    
    SET( HWQ_LIB ${QT_LIBRARIES} )
    ADD_EXECUTABLE(HelloWorldQt ${HWQ_SRC} )
    TARGET_LINK_LIBRARIES(HelloWorldQt ${HWQ_LIB} )
    
    INSTALL_TARGETS( /bin HelloWorldQt)"
    
    1. So I want to incorporate these two files as one, and combine the main.cpp and add #include <qtgui qapplication=""> #include "MainWindow.h" #include <qlabel> #include <qstring> #include <qvboxlayout> in teleop_turtle_joy.cpp, but I find no matter what sequences I put these CMakeLists files, it can run CMake, but when I build, Qt creator show " error: undefined reference to QMainWindow::QMainWindow(QWidget*, QFlags<Qt::WindowType>); error: undefined reference tovtable for MainWindow" and etc,

For the second kind of error, it because lacking moc files, so my incorporated cMakeList does not produce moc files, but why? It looks that combined CMakeList.txt really run on the ROS, but it does not produce the same file as when I only run HelloWidget, which produces the moc files for two **.cpp. So can you point one or more direction for me?

the corporated CMakeList.txt "

        cmake_minimum_required(VERSION 2.4.6)
        PROJECT(learning_joy)

        # Set ...
(more)
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
9

answered 2012-01-27 10:36:58 -0500

dornhege gravatar image

That looks quite chaotic. Actually it shouldn't be so hard.

First: You need to start from the ROS side, i.e. create a normal non-qt ROS package first.

Now you only need some minor tweaks to the CMakeLists.txt.

After rosbuilt_init() I usually put:

find_package(Qt4 REQUIRED)
# enable/disable some Qt features
set( QT_USE_QTGUI TRUE )
set( QT_USE_QTOPENGL TRUE )
set( QT_USE_QTXML TRUE )
include(${QT_USE_FILE})

ADD_DEFINITIONS(-DQT_NO_KEYWORDS)
Qt specific files might need to be moc'd. This includes the headers, so put those here:
set(qt_srcs
      src/qtfile.cpp)
set(qt_hdrs
      src/qtfile.h)
qt4_automoc(${qt_srcs})
QT4_WRAP_CPP(qt_moc_srcs ${qt_hdrs})
Finally GUI files (.ui) might need to be processed:
QT4_WRAP_UI(uis_h src/qtfile.ui)
# include this for ui_h
include_directories(${CMAKE_CURRENT_BINARY_DIR})
When you actually build the executable, make sure you include the files processed above:
rosbuild_add_executable(qttest src/myMain.cpp
    ${uis_h} ${qt_srcs} ${qt_moc_srcs})
target_link_libraries(qttest ${QT_LIBRARIES})
edit flag offensive delete link more

Comments

That "Actually it shouldn't be so hard" is so reassuring !

2ROS0 gravatar image 2ROS0  ( 2016-08-04 09:27:38 -0500 )edit
0

answered 2012-01-27 14:49:00 -0500

tairen gravatar image

updated 2012-01-27 14:50:55 -0500

dornhege,thanks very much!! Your reply lets me know about how to incorporate ROS and Qt, and the errors on my first post disappear, but it exists special errors:

              error: expected ‘:’ before ‘slots’;;
              error: ‘slots’ does not name a type
              error: ‘signals’ does not name a type

However, in the definition of this class, I do have declare the " Q_OBJECT " And the head file is:

                  #include <QLabel>
                  #include <QString>
                  #ifndef HELLOWIDGET_H_
                  #define HELLOWIDGET_H_

                  class HelloWidget : public QLabel {
                              Q_OBJECT
                    public:
                          HelloWidget(QWidget * parent = 0);
                          ~HelloWidget();

                    public slots:
                              void showNewLetter();

                    protected:
                          QString _msg;
                          unsigned short _pos;
                    signals:
                              void showNewLetter1(); //just for test
                    };
                   #endif /* HELLOWIDGET_H_ */

According to the answer, my new CMakeList.txt is:

              cmake_minimum_required(VERSION 2.4.6)
              include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

               rosbuild_init()

              find_package(Qt4 REQUIRED)
              # enable/disable some Qt features
               set( QT_USE_QTGUI TRUE )
               set( QT_USE_QTOPENGL TRUE )
               set( QT_USE_QTXML TRUE )
               include(${QT_USE_FILE})

               ADD_DEFINITIONS(-DQT_NO_KEYWORDS)

               set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
               #set the default path for built libraries to the "lib" directory
               set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

               set(qt_srcs
                         HelloWidget.cpp
                         MainWindow.cpp
                    )
               set(qt_hdrs
                         HelloWidget.h
                          MainWindow.h
                    )
               qt4_automoc(${qt_srcs})
               QT4_WRAP_CPP(qt_moc_srcs ${qt_hdrs})

               QT4_WRAP_UI(uis_h HelloWidget.ui)

                # include this for ui_h
                include_directories(${CMAKE_CURRENT_BINARY_DIR})

                #uncomment if you have defined messages
                 rosbuild_genmsg()

               rosbuild_add_executable(turtle_teleop_joy teleop_turtle_joy.cpp
                          ${uis_h} ${qt_srcs} ${qt_moc_srcs})

               target_link_libraries(turtle_teleop_joy ${QT_LIBRARIES})

Why I has this strange errors on my compiler, is because I lack of some Qt features, but I do not find any other features on web, or anywhere setting on my CMakeList.txt or the configuration of my Qt creator? Can anyone give me a hint? Thanks all the same!

edit flag offensive delete link more

Comments

The reason is the define I added for QT_NO_KEYWORDS. Either remove that or replace slots with Q_SLOTS, etc.
dornhege gravatar image dornhege  ( 2012-01-28 02:37:00 -0500 )edit
dornhege,thanks again!! :-) Then, my program goes well, Oh Yeah!
tairen gravatar image tairen  ( 2012-01-28 03:39:38 -0500 )edit
2

answered 2012-01-27 15:48:44 -0500

Daniel Stonier gravatar image

The qt_ros stack has some simple support and example packages for qt-ros development. Also qt_create has a python script (ala roscreate-pkg style) that will build you a working qt-ros package immediately and you can extend from there.

edit flag offensive delete link more

Comments

I will try when I install the electric. I have install the eros stack on my diamondback version, but I can not run command:roscreate-qt-pkg qtest on my diamondback, even I add the " export PATH=`rospack find eros_python_tools`/scripts:\${PATH} " to setup.sh, so I post my first question.thanks!
tairen gravatar image tairen  ( 2012-01-28 04:10:39 -0500 )edit
What error are you getting? Do you really have a '\' in the "...:\${PATH}" above?
Daniel Stonier gravatar image Daniel Stonier  ( 2012-01-28 22:06:33 -0500 )edit
roscreate-qt-pkg commands not find, :-), actually is my fault. But be careful, when I install correctly, I can't type other commands, like ls, sudo, maybe in embeded edition does not have this commands, so I go to ubuntu recover mode to delete the "export PATH...." , so my system is going well.
tairen gravatar image tairen  ( 2012-01-29 14:09:54 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2012-01-27 07:43:00 -0500

Seen: 12,775 times

Last updated: Jan 27 '12