Qt 显示连续图像流的有效方法

Qt 显示连续图像流的有效方法,qt,Qt,我目前正在使用QLabel来执行此操作,但这似乎相当缓慢: void Widget::sl_updateLiveStreamLabel(spImageHolder_t _imageHolderShPtr) //slot { QImage * imgPtr = _imageHolderShPtr->getImagePtr(); m_liveStreamLabel.setPixmap( QPixmap::fromImage(*imgPtr).scaled(this->si

我目前正在使用QLabel来执行此操作,但这似乎相当缓慢:

void Widget::sl_updateLiveStreamLabel(spImageHolder_t _imageHolderShPtr) //slot
{
    QImage * imgPtr = _imageHolderShPtr->getImagePtr();
    m_liveStreamLabel.setPixmap( QPixmap::fromImage(*imgPtr).scaled(this->size(), Qt::KeepAspectRatio, Qt::FastTransformation) );
    m_liveStreamLabel.adjustSize();
}
这里,我为每个到达的新图像生成一个新的QPixmap对象。由于QPixmap操作仅限于GUI线程,这也会使GUI感觉响应性差。
我看到已经有一些关于这方面的讨论,其中大多数建议使用QGraphicsView或QGLWidget,但我还没有找到一个如何正确使用它们的快速示例,这正是我想要的。

非常感谢您的帮助。

我建议不要使用
QLabel
,而是编写自己的类。每次调用
setPixmap
都会导致布局系统重新计算项目的大小,这可能会传播到最顶层的父级(
QMainWindow
),这是相当大的开销

转换和缩放也有点昂贵


最后,最好的方法是使用探查器来检测最大的问题在哪里。

设置QGraphicsView和QGraphicscene非常简单:-

int main( int argc, char **argv )
{
    QApplication app(argc, argv);

    // Create the scene and set its dimensions
    QGraphicsScene scene;
    scene.setSceneRect( 0.0, 0.0, 400.0, 400.0 );

    // create an item that will hold an image
    QGraphicsPixmapItem *item = new QGraphicsPixmapItem(0);

    // load an image and set it to the pixmapItem
    QPixmap pixmap("pathToImage.png") // example filename pathToImage.png
    item->setPixmap(pixmap);

    // add the item to the scene
    scene.addItem(item);

    item->setPos(200,200); // set the item's position in the scene

    // create a view to look into the scene
    QGraphicsView view( &scene );
    view.setRenderHints( QPainter::Antialiasing );
    view.show();

    return app.exec();
}

QPixmap::fromImage
不是唯一的问题。也应避免使用
QPixmap::scaled
QImage::scaled
。但是,您不能直接在
QLabel
QGraphicsView
中显示
QImage
。下面是我的类,它直接显示
QImage
,并将其缩放到小部件的大小:

标题:

class ImageDisplay : public QWidget {
  Q_OBJECT
public:
  ImageDisplay(QWidget* parent = 0);
  void setImage(QImage* image);

private:
  QImage* m_image;

protected:
  void paintEvent(QPaintEvent* event);

};
资料来源:

ImageDisplay::ImageDisplay(QWidget *parent) : QWidget(parent) {
  m_image = 0;
  setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}

void ImageDisplay::setImage(QImage *image) {
  m_image = image;
  repaint();
}

void ImageDisplay::paintEvent(QPaintEvent*) {
  if (!m_image) { return; }
  QPainter painter(this);
  painter.drawImage(rect(), *m_image, m_image->rect());
}

我测试了3000x3000图像缩小到600x600大小。它的速度为40 FPS,而
QLabel
QGraphicsView
(即使启用了快速图像转换)的速度为15 FPS。

转换和缩放比更新布局要昂贵得多。如何为此更新图像?