目录
已安装好ROS-melodic环境并配置好vscode的环境
qt开发环境配置
sudo apt-get install qtcreator cmake qt5-default
sudo apt-get install ros-melodic-qt-create
sudo apt-get install ros-melodic-qt-build
创建qt功能包
catkin_create_qt_pkg 功能包名 依赖项
比如,在工作空间的src目录下:
catkin_create_qt_pkg class1_ros_qt_demo roscpp rviz
配置qt功能包
创建好功能包后,如果直接用catkin_make指令编译会出现以下报错:
-- ==> add_subdirectory(class1_ros_qt_demo)
-- Found unsuitable Qt version "5.9.5" from /usr/bin/qmake
Qt QTGUI library not found.
Qt QTCORE library not found.
CMake Error at class1_ros_qt_demo/CMakeLists.txt:36 (QT4_ADD_RESOURCES):
Unknown CMake command "QT4_ADD_RESOURCES".
-- Configuring incomplete, errors occurred!
因为新建的功能包的CMakeLists.txt文件生成的时候默认使用QT4版本,而为安装是QT的时候版本是5.9.5,所以会提示找不到QT4的相关依赖包。
修改cmakelist文件:
添加:
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 REQUIRED Core Widgets)
set(QT_LIBRARIES Qt5::Widgets)
删除:
rosbulid_prepare_qt4(QtCore QtGui)
修改:
# QT4_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
# QT4_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
# QT4_WRAP_CPP(QT_MOC_HPP ${QT_MOC})
QT5_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
QT5_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
QT5_WRAP_CPP(QT_MOC_HPP ${QT_MOC})
再次编译,出现以下错误:
/home/robotarm/catkin_qt/src/class1_ros_qt_demo/src/../include/class1_ros_qt_demo/main_window.hpp:15:10: fatal error: QtGui/QMainWindow: 没有那个文件或目录
#include <QtGui/QMainWindow>
^~~~~~~~~~~~~~~~~~~
compilation terminated.
class1_ros_qt_demo/CMakeFiles/class1_ros_qt_demo.dir/build.make:103: recipe for target 'class1_ros_qt_demo/CMakeFiles/class1_ros_qt_demo.dir/src/main_window.cpp.o' failed
make[2]: *** [class1_ros_qt_demo/CMakeFiles/class1_ros_qt_demo.dir/src/main_window.cpp.o] Error 1
CMakeFiles/Makefile2:925: recipe for target 'class1_ros_qt_demo/CMakeFiles/class1_ros_qt_demo.dir/all' failed
make[1]: *** [class1_ros_qt_demo/CMakeFiles/class1_ros_qt_demo.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
在生成的包中的include文件夹下找到main_window.hpp文件
将
#include <QtGUI/QMainWindow>
改为
#include <QtWidgets/QMainWindow>
编译成功。
测试运行
开启roscore,把终端环境source好
rosrun class1_ros_qt_demo class1_ros_qt_demo
将ROS Master Url设置为本机地址:
http://127.0.0.1:11311/
Ros IP设置为
127.0.0.1
点击Connect 实现qt和ROS通信
[ INFO] [1638509596.802866806]: I sent: hello world 0
QObject::connect: Cannot queue arguments of type 'QVector<int>'
(Make sure 'QVector<int>' is registered using qRegisterMetaType().)
[ INFO] [1638509597.801245950]: I sent: hello world 1
[ INFO] [1638509598.801281293]: I sent: hello world 2
[ INFO] [1638509599.801204949]: I sent: hello world 3
[ INFO] [1638509600.801091702]: I sent: hello world 4
[ INFO] [1638509601.801297593]: I sent: hello world 5
[ INFO] [1638509602.801174564]: I sent: hello world 6
[ INFO] [1638509603.801219151]: I sent: hello world 7
[ INFO] [1638509604.801355199]: I sent: hello world 8
[ INFO] [1638509605.801256716]: I sent: hello world 9
[ INFO] [1638509606.801100485]: I sent: hello world 10
[ INFO] [1638509607.801162058]: I sent: hello world 11
[ INFO] [1638509608.801168747]: I sent: hello world 12