【问题标题】:QLabel doesn't display QPixmapQLabel 不显示 QPixmap
【发布时间】:2017-10-02 14:09:54
【问题描述】:

我正在尝试使用以下代码在 QLabel 中显示视频帧,但不幸的是,视频未显示在 QLabel 上。我已将 QAbstractVideoSurface 继承到 CameraFrameGrabber。

bool CameraFrameGrabber::present(const QVideoFrame &frame)
{

    qDebug() << __FUNCTION__;
    if (frame.isValid()) {
        QVideoFrame cloneFrame(frame);
        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
        const QImage image(cloneFrame.bits(),
            cloneFrame.width(),
            cloneFrame.height(),
            QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
        if (MainWindow* child = dynamic_cast<MainWindow*>(this)) {
            //QGraphicsScene *scene = new QGraphicsScene(this);
            //scene->addPixmap(QPixmap::fromImage(image));
            //scene->setSceneRect(image.rect());
            child->ui->label->setPixmap(QPixmap::fromImage(image));

            child->ui->label->update();
            //child->ui->graphicsView->setScene(scene);
            //child->ui->graphicsView->update();

        }
        //emit frameAvailable(image);
        cloneFrame.unmap();
        return true;
    }
    return false;

}

【问题讨论】:

  • 你有没有试过调试看看图片是否包含真实数据?
  • 我发现问题出在 QVideoFrame 的格式上,它无法在 QLabel 中显示。有什么建议吗?

标签: qt video-streaming qlabel qpixmap


【解决方案1】:

问题在于 VideoFrame 格式与 QLabel pixmap 格式不匹配,并且还从主窗口中删除了动态 Cast 并在 Cameraframegrabber 中添加了标签。

QImage outImage = image.convertToFormat(QImage::Format_RGB888);
    myLabel->setPixmap(QPixmap::fromImage(outImage));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多