0

我正在尝试显示模拟器拍摄的视频。我正在通过包含其头文件来实现 ROS 中的 QT 代码。我的代码正在运行。问题:: 每次打开新窗口以显示框架。我保留了 cvWaitKey(10000) 以便在一段时间延迟后出现新窗口。但是更新的框架应该在同一个窗口内。请建议我该怎么做?我的代码如下:

   void imageCallback( const sensor_msgs::ImageConstPtr& msg) //const sensor_msgs::ImageConstPtr&
{   
    imagePublisher.publish (cv_ptr->toImageMsg());                                                      

    QImage temp(&(msg->data[0]), msg->width, msg->height, QImage::Format_RGB888);

    QImage image;
    image=temp;
    // QT Layout with button and image  

    static QWidget *window = new QWidget;
    static QLabel *imageLabel= new QLabel;
    static QPushButton* quitButton= new QPushButton("Quit"); 
    static QPushButton* exitButton= new QPushButton("Exit Image");  
    QVBoxLayout* layout= new QVBoxLayout;


    imageLabel->setPixmap(QPixmap::fromImage(image));
    layout->addWidget(imageLabel);
    layout->addWidget(exitButton);      
    layout->addWidget(quitButton);  

    window->setLayout(layout);
    QObject::connect(quitButton, SIGNAL(clicked()),window, SLOT(close()));  // Adding Buttons
    QObject::connect(exitButton, SIGNAL(clicked()),imageLabel, SLOT(close()));

    window->show();
    cvWaitKey(1);


}

int main(int argc, char **argv) {

   ros::init(argc, argv, "imageSelectNode");
   ros::NodeHandle n("~");




   image_transport::ImageTransport it(n);    
   image_transport::Subscriber sub = it.subscribe("/camera/image_raw",1, imageCallback);

   imagePublisher = it.advertise("imagePublisherTopic", 1);     //Publish the image in 'imagePublisherTopic' node



QApplication a(argc, argv);

    ros::spin();
 return 0;

}

4

1 回答 1

2

将打开一个新窗口,因为您QLabel在每一帧上都创建了一个新窗口。您需要的是 - 一个QLabel,您应该更改哪个像素图。最简单的方法是让你的imageLabel静态:

static QLabel *imageLabel = new QLabel;

更新

如果您想对该标签进行一次操作(例如将其添加到布局中),您可以执行以下操作:

QLabel * createLabel()
{
    QLabel *l = new QLabel;
    layout->addWidget(l);
    return l;
}

...

static QLabel *imageLabel = createLabel();

更新 4

QLabel * createLabel()
{
    QWidget *window = new QWidget;
    QLabel *imageLabel= new QLabel;
    QPushButton* quitButton= new QPushButton("Quit"); 
    QPushButton* exitButton= new QPushButton("Exit Image");  
    QVBoxLayout* layout= new QVBoxLayout;

    layout->addWidget(imageLabel);
    layout->addWidget(exitButton);      
    layout->addWidget(quitButton);  

    window->setLayout(layout);
    QObject::connect(quitButton, SIGNAL(clicked()),window, SLOT(close()));
    QObject::connect(exitButton, SIGNAL(clicked()),imageLabel, SLOT(close()));

    window->show();
    return imageLabel;
}

void imageCallback( const sensor_msgs::ImageConstPtr& msg)
{
    imagePublisher.publish (cv_ptr->toImageMsg());

    QImage temp(&(msg->data[0]), msg->width, msg->height, QImage::Format_RGB888);

    QImage image;
    image = temp;

    static QLabel *imageLabel = createLabel();
    imageLabel->setPixmap(QPixmap::fromImage(image));
    cvWaitKey(1);
}
于 2013-05-24T17:10:09.503 回答