ROS与Qt5人机交互界面开发-配置QT环境
ROS与Qt5人机交互界面开发-配置QT环境
说明:
- 介绍如何搭配QT环境
步骤:
- 第一步:安装ROS,参考教程
- 第二步:安装qt pkg的支持
sudo apt-get install ros-melodic-qt-create
sudo apt-get install ros-melodic-qt-build
- 完成之后就可以通过catkin_create_qt_pkg来创建ros gui包
mkdir -p ~/catkin_qt/src
catkin_create_qt_pkg qt_ros_test
- 在工作空间下catkin_qt/src创建包qt_ros_test
- 文件目录如下:
wget http://download.qt.io/archive/qt/5.9/5.9.9/qt-opensource-linux-x64-5.9.9.run
chmod a+x qt-opensource-linux-x64-5.9.9.run
./qt-opensource-linux-x64-5.9.9.run
- 根据默认提示安装就可以
- 添加qtcreator到环境变量
sudo nano /usr/bin/qtcreator
- 内容如下:
#!/bin/sh
export QT_HOME=/home/xxxx/Qtxxx/Tools/QtCreator/bin
$QT_HOME/qtcreator $*
- 更改权限
sudo chmod a+x /usr/bin/qtcreator
- 终端输入qtcreator即可打开
qtcreator
- 第四步:配置qtcreator
- 文件->打开项目->选择工作空间下的cmakelist.txt(注意并非功能包下的)
- 打开后点击项目:
- 将build的构建目录改为当前工作空间的build文件夹
- 然后右键项目->执行cmake:
- 如果发现报版本不匹配的错误(因为创建的为qt4的版本,这里用的是qt5)
Found unsuitable Qt version "" from NOTFOUND
CMake Error at ros_qt_tests/CMakeLists.txt:36 (QT4_ADD_RESOURCES):
Unknown CMake command "QT4_ADD_RESOURCES".
- 则继续往下看2,3步,如果没有直接跳过下面两步:
- 将功能包的cmakelist.txt进行更改:
- 更改为如下,注意将cy_robot_monitor更改为自己的功能包名
##############################################################################
# CMake
##############################################################################
cmake_minimum_required(VERSION 2.8.0)
project(cyrobot_monitor)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
##############################################################################
# Catkin
##############################################################################
# qt_build provides the qt cmake glue, roscpp the comms for a default talker
find_package(catkin REQUIRED COMPONENTS rviz roscpp)
find_package(Qt5 REQUIRED Core Widgets)
set(QT_LIBRARIES Qt5::Widgets)
include_directories(${catkin_INCLUDE_DIRS})
# Use this to define what the package will export (e.g. libs, headers).
# Since the default here is to produce only a binary, we don't worry about
# exporting anything.
catkin_package()
##############################################################################
# Qt Environment
##############################################################################
# this comes from qt_build's qt-ros.cmake which is automatically
# included via the dependency call in package.xml
#rosbuild_prepare_qt4(QtCore QtGui) # Add the appropriate components to the component list here
##############################################################################
# Sections
##############################################################################
file(GLOB QT_FORMS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ui/*.ui)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources/*.qrc)
file(GLOB_RECURSE QT_MOC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS include/cyrobot_monitor/*.hpp *.h)
QT5_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})
QT5_WRAP_UI(QT_FORMS_HPP ${QT_FORMS})
QT5_WRAP_CPP(QT_MOC_HPP ${QT_MOC})
##############################################################################
# Sources
##############################################################################
file(GLOB_RECURSE QT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} FOLLOW_SYMLINKS src/*.cpp)
##############################################################################
# Binaries
##############################################################################
add_executable(cyrobot_monitor ${QT_SOURCES} ${QT_RESOURCES_CPP} ${QT_FORMS_HPP} ${QT_MOC_HPP})
target_link_libraries(cyrobot_monitor ${QT_LIBRARIES} ${catkin_LIBRARIES})
install(TARGETS cyrobot_monitor RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})
- 功能包的package.xml更改:
- 注意将cy_robot_monitor更改为自己的功能包名
<?xml version="1.0"?>
<package>
<name>cyrobot_monitor</name>
<version>0.1.0</version>
<description>
cyrobot_monitor
</description>
<maintainer email="chengyangkj@gmail.com">chengyangkj</maintainer>
<author>chengyangkj</author>
<license>BSD</license>
<!-- <url type="bugtracker">https://github.com/stonier/qt_ros/issues</url> -->
<!-- <url type="repository">https://github.com/stonier/qt_ros</url> -->
<buildtool_depend>catkin</buildtool_depend>
<!--build_depend>qt_build</build_depend-->
<build_depend>roscpp</build_depend>
<!--build_depend>libqt4-dev</build_depend-->
<!--run_depend>qt_build</run_depend-->
<run_depend>roscpp</run_depend>
<!--run_depend>libqt4-dev</run_depend-->
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
<build_depend>rospy</build_depend>
<run_depend>rospy</run_depend>
<build_depend>rviz</build_depend>
<run_depend>rviz</run_depend>
<build_depend>qtbase5-dev</build_depend>
<run_depend>libqt5-core</run_depend>
<run_depend>libqt5-gui</run_depend>
<run_depend>libqt5-widgets</run_depend>
</package>
保存再次执行cmake, 即可成功
更改功能包main_window.hpp
由于Q4t中包含QMainWindow在QtGui中,在Qt5中更改为QtWidgets类中,所以需要作更改:
./include/功能包名/main_window.hpp
修改如下:
#include <QtGui/QMainWindow>
变更为:
#include <QtWidgets/QMainWindow>
- 点击左下角的三角标识符,进行编译运行
- 正常情况下,弹出界面
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号