我正在 ROS 节点中实现 Qt 代码。我有一个头文件,其中定义了所有成员 Q_SIGNAL 和 Q_SLOTS。在我的 .cpp 文件中,我想在按下按钮(assignButton)时显示图像。但是当我按下按钮时,什么也没有出现。
为了测试连接功能是否正常工作,我尝试在存储在我的笔记本电脑中的 imageLabel 中显示图像......它工作正常。
问题:-我正在通过 ROS 从模拟器中获取图像
void SelectionInterface::imageCallback(const sensor_msgs::ImageConstPtr& msg)
,我想通过 SIGNAL-SLOT 在 imageLabel 中显示这些图像..但它没有显示..没有错误
我的代码如下: -
1.头文件--SelectionInterface.h
class SelectionInterface : public QMainWindow
{
Q_OBJECT
public:
SelectionInterface(ros::NodeHandle *nh, RosThread *rt, QMainWindow *parent = 0);
~SelectionInterface();
private:
RosThread *rosThread;
ros::NodeHandle *nodeHandle;
// ROS Subscribers
image_transport::Subscriber image_sub;
void imageCallback(const sensor_msgs::ImageConstPtr& msg);
// More Memberfunctions and Variables
QWidget *newCentralWidget;
QPushButton *quitButton;
QPushButton *assignButton;
QVBoxLayout *layout;
QLabel *imageLabel;
QImage image;
// ...
protected:
void closeEvent(QCloseEvent *event);
Q_SIGNALS:
void windowClosedSignal();
private Q_SLOTS:
void quitInterface();
void assignImage();//QImage
};
2..cpp文件
#include "SelectionInterface.h"
#include <iostream>
SelectionInterface::SelectionInterface(ros::NodeHandle *nh, RosThread *rt, QMainWindow *parent): QMainWindow (parent)
{
rosThread = rt;
nodeHandle = nh;
// Subscribing and Publishing
image_transport::ImageTransport it(*nh);
// Setup user interface here
newCentralWidget = new QWidget;
quitButton = new QPushButton("Quit");
assignButton = new QPushButton("Assign Image");
layout = new QVBoxLayout;
imageLabel = new QLabel;
imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addWidget(imageLabel);
layout->addWidget(quitButton);
layout->addWidget(assignButton);
newCentralWidget->setLayout(layout);
this->setCentralWidget(newCentralWidget);
// Signal-Slot Connections
connect(this, SIGNAL(windowClosedSignal()), this, SLOT(quitInterface()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(assignButton, SIGNAL(clicked()), this, SLOT(assignImage()));
// ...
}
void SelectionInterface::imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
QImage image(&(msg->data[0]), msg->width, msg->height, QImage::Format_RGB888);
}
SelectionInterface::~SelectionInterface()
{
//destructer (leave empty)
}
void SelectionInterface::closeEvent(QCloseEvent *event)
{
Q_EMIT windowClosedSignal();
}
void SelectionInterface::quitInterface()
{
rosThread->stop();
rosThread->wait();
std::cout << "Good bye.\n";
}
void SelectionInterface::assignImage()
{
imageLabel->setPixmap(QPixmap::fromImage(image));
}