# ROS2: running command inside Qt?

**URL:** <https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673>\
**Category:** ROS General\
**Created:** [September 17, 2017, 1:42pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673 "2017-09-17T13:42:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kloping](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kloping/32/3471_2.png) [@Kloping](https://discourse.openrobotics.org/u/Kloping)\
**Post date:** [September 17, 2017, 1:42pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/1 "2017-09-17T13:42:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wjwwood](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/wjwwood/32/13715_2.png) [@wjwwood](https://discourse.openrobotics.org/u/wjwwood)\
**Post date:** [September 18, 2017, 7:01am UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/2 "2017-09-18T07:01:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![firesurfer](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/firesurfer/32/11626_2.png) [@firesurfer](https://discourse.openrobotics.org/u/firesurfer)\
**Post date:** [September 18, 2017, 9:34am UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/3 "2017-09-18T09:34:36Z")

</div>

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 - #5 by dirk-thomas - ROS General - Open Robotics Discourse](http://discourse.ros.org/t/trouble-opening-package-in-qtcreator/2196/5))  
and then start qtcreator in this terminal.

In the first case you have to adjust your CMakeLists.txt (For example like that: [ros2\_components/CMakeLists.txt at master · firesurfer/ros2\_components · GitHub](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 ( [ros2\_components/src/ros2\_components/CMakeLists.txt at master · firesurfer/ros2\_components · GitHub](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

)

---

<div class="post-metadata">

**Author:** ![Kloping](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kloping/32/3471_2.png) [@Kloping](https://discourse.openrobotics.org/u/Kloping)\
**Post date:** [September 18, 2017, 1:22pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/4 "2017-09-18T13:22:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![firesurfer](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/firesurfer/32/11626_2.png) [@firesurfer](https://discourse.openrobotics.org/u/firesurfer)\
**Post date:** [September 18, 2017, 1:37pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/5 "2017-09-18T13:37:59Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![Kloping](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kloping/32/3471_2.png) [@Kloping](https://discourse.openrobotics.org/u/Kloping)\
**Post date:** [September 18, 2017, 1:52pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/6 "2017-09-18T13:52:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![firesurfer](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/firesurfer/32/11626_2.png) [@firesurfer](https://discourse.openrobotics.org/u/firesurfer)\
**Post date:** [September 18, 2017, 1:57pm UTC](https://discourse.openrobotics.org/t/ros2-running-command-inside-qt/2673/7 "2017-09-18T13:57:21Z")

</div>

Then sourcing before is the easiest possibility.  
See [https://stackoverflow.com/questions/30246742/setting-environment-variables-at-qt-creator-by-sourcing-a-shell-script](https://stackoverflow.com/questions/30246742/setting-environment-variables-at-qt-creator-by-sourcing-a-shell-script)

Otherwise:

> [@firesurfer](#):
>
> In qtcreator (the ide) there is somewhere at the project tab the possibilty to set a custom build / run environment.

[https://doc-snapshots.qt.io/qtcreator-4.0/creator-build-settings.html](https://doc-snapshots.qt.io/qtcreator-4.0/creator-build-settings.html)
