【发布时间】:2020-02-07 18:40:55
【问题描述】:
我无法将用 C++ 创建的 QCamera 显示到 QML 中的 VideoOutput。 如果我在 QML 中使用相机的这种方式,一切都很好,我得到了视频输出:
Item{
VideoOutput
{
id: videoOutput
anchors.fill: parent
source: camera
}
Camera
{
id: camera
}
}
但在我的情况下,相机不在 QML 中。我正在用 C++ 创建它。我试图在 C++ 中创建它并将其设置为 contextProperty,因此在 qml 的 VideoOutput 的源代码中使用它。所以这是我的 main.cpp。
QCamera* camera;
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras)
{
qDebug() << cameraInfo.description();
camera = new QCamera(cameraInfo);
}
if(camera)
{
qDebug() << "setContextProperty camera ";
engine.rootContext()->setContextProperty("mCamera", camera);
}
一切都很好,摄像头被检测到,我在 QML 中使用它,如下所示:
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item
{
id: cameraView
height: 230
width: 300
anchors.centerIn: parent
VideoOutput
{
id: videoOutput
anchors.fill: parent
source: mCamera
}
}
但是这种方式没有视频输出。这有可能实现吗?提前致谢。
【问题讨论】:
-
您是否按照documentation 中的说明进行操作?:如果您正在扩展自己的 C++ 类以与 VideoOutput 互操作,您可以提供基于 QObject 的类,该类具有 mediaObject 属性公开具有可用 QVideoRendererControl 的 QMediaObject 派生类,或者您可以提供具有可写 videoSurface 属性的基于 QObject 的类,该属性可以接受基于 QAbstractVideoSurface 的类,并且可以遵循正确的协议将 QVideoFrames 传递给它。
-
我会检查并尝试一下。非常感谢@folibis
-
链接文档适用于您想要显示常规相机以外的其他内容时