ROS与Qt冲突
-
3.4.5订阅数据并用Qt显示
按照书本编写程序,运行节点的时候报警:
QSocketNotifier: Can only be used with threads started with QThread
/home/mr/ros2/chapt3/topic_practice_ws/install/status_display/lib/status_display/sys_status_display: symbol lookup error: /snap/core20/current/lib/x86_64-linux-gnu/libpthread.so.0: undefined symbol: __libc_pthread_init, version GLIBC_PRIVATE
[ros2run]: Process exited with failure 127以下为代码:
#include <QApplication>
#include <QLabel>
#include <QString>
#include "rclcpp/rclcpp.hpp"
#include "status_interfaces/msg/system_status.hpp"
using SystemStatus = status_interfaces::msg::SystemStatus;```class SysStatusDisplay : public rclcpp::Node{
public:
SysStatusDisplay() : Node("sys_status_display"){
subscription_ =this->create_subscription<SystemStatus>(
"sys_status",10,[&](const SystemStatus::SharedPtr msg) -> void{
label_ -> setText(get_qstr_from_msg(msg));
});
//创建一个空的SystemStatus 对象,转换成QS人tring 进行显示
label_ = new QLabel(get_qstr_from_msg(std::make_shared<SystemStatus>()));
label_->show();
}
QString get_qstr_from_msg(const SystemStatus::SharedPtr msg){
//TODO: 将msg中的内容提出出来并组装成字符串
std::stringstream show_str;
show_str
<<"===========系统状态可视化显示工具===========\n"
<< "数据时间\t" << msg->stamp.sec << "\ts\n"
<< "用 户 名\t" << msg->host_name << "\t\n"
<< "CPU使用率\t" <<msg->cpu_percent << "\t%\n"
<< "内存使用率\t" <<msg->memory_percent << "\t%\n"
<< "内存总大小\t" << msg->memory_total <<"\tMB\n"
<< "剩余有效内存\t" << msg->memory_available << "\tMB\n"
<< "网络发送量\t" <<msg->net_sent <<"\tMB\n"
<< "网络接收量\t"<<msg->net_recv << "\tMB\n"
<< "=========================================";
return QString::fromStdString(show_str.str());
}
private:
rclcpp::Subscription<SystemStatus>::SharedPtr subscription_;
QLabel* label_;
};int main(int argc,char* argv[]){
rclcpp::init(argc,argv);
QApplication app(argc,argv);
auto node = std::make_shared<SysStatusDisplay>();
std::thread spin_thread(& -> void
{
rclcpp::spin(node);
});
spin_thread.detach();
app.exec();
rclcpp::shutdown();
return 0;
}===========================================================================================================
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(status_display)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(status_interfaces REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Widgets)add_executable(hello_qt src/hello_qt.cpp)
add_executable(sys_status_display src/sys_status_display.cpp)
target_link_libraries(sys_status_display Qt5::Widgets) #对于非ROS功能使用Cmake原生指令进行链接库
ament_target_dependencies(sys_status_display rclcpp status_interfaces)
target_link_libraries(hello_qt
Qt5::Widgets
)if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
source files
set(ament_cmake_copyright_FOUND TRUE)
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()install(TARGETS
hello_qt
sys_status_display
DESTINATION lib/${PROJECT_NAME})
ament_package()
===========================================================================================================
package.xml:
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>status_display</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="mr@todo.todo">mr</maintainer>
<license>Apche-2.0</license><buildtool_depend>ament_cmake</buildtool_depend>
<depend>qtbase5-dev</depend>
<depend>rclcpp</depend>
<depend>status_interfaces</depend><test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend><export>
<build_type>ament_cmake</build_type>
</export>
</package>===========================================================================================================
Ubuntu版本:Ubuntu 24.04.2 LTS
ROS2版本:jazzy
问过deepseek和文心一言,文心一言表示可能是因为detach()别用;可以改称spin_thread.join(),可没有效果,反馈后又说别用join和detach。deepseek则用QObject这种还没有接触到的,可能是操作步骤少了或者有问题deepseek方案照抄了也没有用。