在同一窗口中使用QImage显示视频?

时间:2013-05-24 14:50:34

标签: qt qt-creator ros

我正在尝试显示模拟器拍摄的视频。我通过包含其头文件在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;

}

1 个答案:

答案 0 :(得分: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);
}