Qt 在同一窗口中使用QImage显示视频?
我试图显示由模拟器拍摄的视频。我通过包含头文件在ROS中实现QT代码。我的代码正在运行。 问题:每次打开新窗口以显示框架时。我保留了cvWaitKey(10000)以便新窗口在延迟一段时间后出现。但是更新的帧应该在同一个窗口中。请建议我怎么做??我的代码如下:Qt 在同一窗口中使用QImage显示视频?,qt,qt-creator,ros,Qt,Qt Creator,Ros,我试图显示由模拟器拍摄的视频。我通过包含头文件在ROS中实现QT代码。我的代码正在运行。 问题:每次打开新窗口以显示框架时。我保留了cvWaitKey(10000)以便新窗口在延迟一段时间后出现。但是更新的帧应该在同一个窗口中。请建议我怎么做??我的代码如下: void imageCallback( const sensor_msgs::ImageConstPtr& msg) //const sensor_msgs::ImageConstPtr& { imag
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,字符**argv)
{
}将打开一个新窗口,因为您在每个帧上创建了一个新的
QLabel
。您需要的是-oneQLabel
,您应该更改哪个pixmap。最简单的方法是使imageLabel
静态:
static QLabel *imageLabel = new QLabel;
更新:
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);
}
如果要对该标签执行一次操作(如将其添加到布局),可以执行以下操作:
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);
}
为什么要使用一个临时的“image”变量?它起作用了,但我想把这个QLabel添加到我的布局中。因此,通过将QLabel声明为静态,我不断收到一条错误消息“试图更改已设置布局的布局”……虽然程序正在为我提供所需的功能,但我在终端窗口中不断收到此消息。请建议…谢谢你的回复。我已经用代码更新了。请检查一下。问题是,当我订阅相机图像时,我正在从Main()程序调用回调函数。我不明白如何在另一个类中定义callback()。很抱歉,我不明白如何做…标记类的一个成员并“在窗口类的构造函数中创建它”谢谢,我认为你已经为QT creator文件编写了代码。但我不是用QT编写代码。我正在编写一个ROS节点的代码,在那里我使用了一些QT的东西。请检查我的代码..我收到了这个错误“QWidget::setLayout:试图在QWidget“”上设置QLayout“”,它已经有一个布局”我在第一次更新中回答了这个问题。使用静态QLabel*imageLabel=createLabel()代码>构造并将所有gui创建移动到那里。然而,如果不使用Qt的对象范例,这段代码非常难看。