示例
实现方式有很多,这里主要是通过system()实现。
先讲一个示例,下面以运行ros的小乌龟为例:
先新建一个qt项目
然后ui界面设计两个按钮
然后第一个按钮点击右键->选择转到槽
然后在槽函数中写入
system("gnome-terminal -- bash -c 'source /opt/ros/melodic/setup.bash ;roscore'&");
然后在另一个按钮槽函数中写入
system("gnome-terminal -- bash -c 'source /opt/ros/melodic/setup.bash ;rosrun turtlesim turtlesim_node'&");
system("gnome-terminal -- bash -c 'source /opt/ros/melodic/setup.bash ;rosrun turtlesim turtle_teleop_key'&");
效果
点击启动ros
再点击小乌龟就可以动了
说明
gnome-terminal是终端模拟器
– bash -c后面写的是命令
source /opt/ros/melodic/setup.bash 是配置环境变量
如果是自己的工作空间就是
source /home/xx/catkin_ws/devel/setup.bash
其中xx是系统名
catkin_ws是工作空间