ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A

ROS2: running command inside Qt?


I am trying run some ros2 command directly inside Qt, but I don’t know how to set up Qt’s environment or how run the setup.bash file inside Qt?

For example, I tried:
system(qPrintable(“. ~/ros2_install/ros2-osx/setup.bash”)); in Qt, and get the error as following:

env: python3: No such file or directory
error: unable to find fallback python3 executable

I know there is one way that I can source the setup.bash file in Terminal and run Qt from this Terminal, but is there a method to run the setup.bash file directly inside Qt?

I’m not sure what this has to do specifically with ROS 2. The general issue here is how do you source a shell script correctly via a system call.

I recommend asking on a more generic forum. We usually use Python’s subprocess module to do this kind of thing.

Hi,what do you mean by running inside Qt ? Building an application that uses Qt and ROS2 or opening a ROS2 package inside the Qt Creator IDE ? In the second case I’d say it is easier to open a bash. Then source the ros2_ws and your own workspace. (See also Trouble opening package in qtcreator)
and then start qtcreator in this terminal.

In the first case you have to adjust your CMakeLists.txt (For example like that:

The relevant lines are:
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5Widgets REQUIRED)


And in the CMakeLists that acutally adds targets (






What I meant is just like what wjwwood writes: I need to source a shell script via a system call inside Qt.

Your method of sourcing the terminal and then open qt with this terminal works for sure, but do you have any experience to run the “sourcing command” directly in Qt using system call?

If not, then I guess I’ll ask this in a more generic forum like he said.

But still, thanks for your answer!

My question is still. Do you mean qt or qtcreator? Thats a huge difference. In qtcreator (the ide) there is somewhere at the project tab the possibilty to set a custom build / run environment.

Doing a systemcall for sourcing doesn’t make any sense. But running a qt application together with ros2 without sourcing before is also possible.
The script doesn’t do anything else than setting some environment variables.
You could set the variables by hand using:

Edit: See why running source via a systemcall doesnt work and also doesn’t make sense.

oh sorry I didn’t understand that, I meant qtCreator(the ide).

Then sourcing before is the easiest possibility.