使用ROS节点中的SIGNAL-SLOT在QLabel中无法显示/更新图像

时间:2013-06-11 17:26:24

标签: multithreading qt qt-creator ros

我正在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));       
}

1 个答案:

答案 0 :(得分:0)

我怀疑QImage可以将~替换为您的主路径。尝试指定完整路径,例如'/home/user/Pictures/wallpapers/buddha.jpg'。