欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > ros2--Qt

ros2--Qt

2025/11/16 21:34:16 来源:https://blog.csdn.net/m0_60274660/article/details/144967093  浏览:    关键词:ros2--Qt

使用案例

三步ros2中使用Qt.

1,CMakeLists.txt配置

cmake_minimum_required(VERSION 3.8)
project(hello_qt)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")add_compile_options(-Wall -Wextra -Wpedantic)
endif()# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
# 重点1:引入Qt库
find_package(Qt5 REQUIRED COMPONENTS Widgets)add_executable(hello_qt_exec src/main.cpp)
# 重点2:链接Qt库
# 这里为什么使用target_link_libraries而不是ament_target_dependencies?
# 因为Qt5这个库并不是ros的功能包库,而ament_target_dependencies是用于快速查找和依赖ros功能包的工具。
target_link_libraries(hello_qt_exec Qt5::Widgets)if(BUILD_TESTING)find_package(ament_lint_auto REQUIRED)# the following line skips the linter which checks for copyrights# comment the line when a copyright and license is added to all source filesset(ament_cmake_copyright_FOUND TRUE)# the following line skips cpplint (only works in a git repo)# comment the line when this package is in a git repo and when# a copyright and license is added to all source filesset(ament_cmake_cpplint_FOUND TRUE)ament_lint_auto_find_test_dependencies()
endif()install(TARGETS hello_qt_execDESTINATION lib/${PROJECT_NAME})ament_package()

2,源码使用Qt

// 重点3:引用Qt头文件
#include <QApplication>
#include <QLabel>
#include <QString>int main(int argc, char **argv) {QApplication app(argc, argv);QLabel *label = new QLabel();label->setText("Hello Qt!");label->show();app.exec();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词