There is already an existing guide to make Qt and ROS work together but, using the "Import Existing Project" tool of QtCreator may not include all your project's libraries, and therefore the advanced editing capabilities of Qt will not be available if these files are not properly linked.
This guide will fix those problems by directly using CMakeLists.txt, this also has other advantage; you could use your project like a normal Qt project (no ROS).
Most of this guide was taken from:
Levi Armstrong
Setup Qt Creator for ROS
NOTE: This or Armstrong's guide will not work if you are using an old Qt version that uses a "CMake wizard" when you create a project, which is shown in the following image:
1.1 Setup Ubuntu to allow debugging/ptrace
1.2 Adding ROS File Types
1.3 Configuring your CMakeLists.txt file
1.4 Opening your project using CMakeLists.txt in Qt
1.5 Setting up Build Settings for Catkin Workspace
1.6 Setting up Run Settings for Catkin Workspace
1.7 Debugging Catkin Workspace
1.7.1 Debugging a single file
1.7.2 Attaching to a running process (Default when working with ROS)
1.8 Opening QtCreator with ubuntu terminal
1.8.1 Going crazy with shortcuts
- Open the file:
sudo gedit /etc/rc.local
- Add this line before the
exit 0
line:
echo 0 | tee /proc/sys/kernel/yama/ptrace_scope
- Reboot your computer
This allows Qt Creator to recognize file types like *.launch as xml for syntax highlighting and specifies that it needs to be included during the project import.
- Open Qt Creator
- Goto: Tools > Options > Environment > MIME Types
- The table below is a start to a list of extension to be added:
MIME Type | Extensions |
---|---|
application/xml | *.launch; *.xacro; *.urdf |
text/plain | *.srv; *.msg |
It is not possible to run CMakeLists.txt in Qt with catkin
libraries (find_package(catkin REQUIRED ...)
). To avoid any error when running CMake you have to add few lines to your CMakeLists.txt file, the next code example shows what you have to do.
...
if(DEFINED CATKIN_DEVEL_PREFIX)
#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––#
# adding all the ROS elements inside this IF conditional #
#––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––#
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
genmsg
message_generation
roscpp
rospy
std_msgs
)
## Generate messages in the 'msg' folder
add_message_files(FILES
some_message.msg
other_message.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(DEPENDENCIES std_msgs)
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS your_project/include
# LIBRARIES state_machine
CATKIN_DEPENDS message_runtime
# DEPENDS system_lib
)
endif(DEFINED CATKIN_DEVEL_PREFIX)
...
# Adding dependencies will not work because the last parameter is not going
# to be defined and you need at least 3 of them, so, you need to put it
# inside the "if(DEFINED CATKIN_DEVEL_PREFIX)" conditional.
if(DEFINED CATKIN_DEVEL_PREFIX)
add_dependencies(${PROJECT_NAME}
${PROJECT_NAME}_generate_messages # example when generating messages
${catkin_EXPORTED_TARGETS}
)
endif(DEFINED CATKIN_DEVEL_PREFIX)
...
You can see a full example of CMakeLists.txt inside the files of this repository. And another example of CMakeLists.txt for CarMaker.
- Goto: File > Open File or Project...
- Open your project's
CMakeLists.txt
file. - The Configure project screen will be displayed, click at Details
- It is recomendable to have only one x option chosen and change the folder path in the same project folder located at the catkin_ws directory. That folder will contain the simulation output's log files and ensures a correct connection between Qt, CarMaker (if used) and ROS. Do not worry about file overwriting because the executable will be located in the devel folder.
- Click in the Configure Project button.
- If the "Project Browser" is not visible press: Alt+0
- First open the project, then navigate to the Projects > Build Settings
- Remove existing "Build Steps".
- Add a custom build step Add Build Step > Custom Process Step as shown below.
- Command:
catkin_make
- Arguments:
-DCMAKE_BUILD_TYPE=Debug
- Working directory:
%{buildDir}
- Command:
- Remove existing "Clean Steps".
- Add a custom clean step Add Clean Step > Custom Process Step as shown below.
- Command:
catkin_make
- Arguments:
clean
- Working directory:
%{buildDir}
- Command:
- First open the project, then navigate to the Projects > Run Settings
- Add a "Run Configuration" by selecting the Add > Custom Executable
- Next select the Browse... associated with the executable field and browse to the executable you wish to debug in the workspace devel/lib directory (You must have previously compiled your project using the Terminal and catkin_make command).
- Now everything should be setup and ready to begin debugging.
- Open a terminal and start a roscore
- Open another terminal and browse to the catkin workspace and source directory.
- Next open qt creator from this terminal using the command below and open project.
- Command:
qtcreator &
- If the previous command does not work follow this step.
- Command:
- Now from the terminal launch the project you wish to debug. (optional: if using launch file)
- Next in qt creator browse to the file you wish to debug and insert break points.
- Now select the start debugger button.
- Note: If you are using launch file this will kill the currently running executable generated from step 4 and start a new one.
- Now depending on where the break points were placed qt should be stopped at a break point.
- Open a terminal and browse to the catkin workspace and source directory.
- Next open qt creator from this terminal using the command below and open project.
- Command:
qtcreator &
- If the previous command does not work follow this step.
- Command:
- Now from the terminal launch the project you wish to debug. (optional: if using launch file)
- Next in Qt Creator browse to the file you wish to debug and insert break points.
- Menu Bar > Debug > Start Debugging > Attach to Running Process
- Now select the Process ID and then click the button Attach to Process.
- Now depending on where the breakpoints were placed in qt, it should be stopped at a break point when it reaches one.
- Note: Sometime it will be paused at a ros::spin, so check after it has attached and if it is passed and not at a inserted breakpoint click the continue button in the debugger.
If you have opened an ubuntu terminal, runned the qtcreator &
command and received the message qtcreator: command not found
you have to follow the next steps to get it working:
- run the command:
gedit ~/.bash_aliases
- Add the line below and do not forget to change
<qt folder>
to the Qt folder's path:
alias qtcreator='<qt folder>/Tools/QtCreator/bin/qtcreator'
- run
source ~/.bashrc
in the terminal - Everything is ready to use the
qtcreator &
command.
You could add more shortcuts for the Ubuntu terminal, some examples:
- Adding a shortcut to catkin_ws folder (assuming ~/catkin_ws/ is your current catkin folder)
alias catkin='cd ~/catkin_ws/; source devel/setup.bash;'
- Adding a shortcut to your catkin_ws project:
alias my_project='cd ~/catkin_ws/src/my_project_folder; source ../../devel/setup.bash;'
- Adding a shortcut to open QtCreator and catkin_ws' folder together (
qtcreator &
must be working already):
alias qt_catkin='cd ~/catkin_ws/; source devel/setup.bash; qtcreator &'