ROS2: running command inside Qt?

Hi,

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: https://github.com/firesurfer/ros2_components/blob/master/CMakeLists.txt)

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

and
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

And in the CMakeLists that acutally adds targets ( https://github.com/firesurfer/ros2_components/blob/master/src/ros2_components/CMakeLists.txt)

include_directories(
${Qt5Core_INCLUDE_DIRS}
${Qt5Network_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}

)

and

target_link_libraries(
Qt5::Core
Qt5::Network
Qt5::Widgets

)

Hi,
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: https://stackoverflow.com/questions/899517/set-local-environment-variables-in-c

Edit: See https://stackoverflow.com/questions/11272720/how-to-run-source-command-in-c 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.
See Setting environment variables at Qt Creator by sourcing a shell script - Stack Overflow

Otherwise:

https://doc-snapshots.qt.io/qtcreator-4.0/creator-build-settings.html