Solved Setting VideoOutput source programmatically
-
Hello,
I'm having trouble setting the source of a VideoOutput component programmatically. My QCamera gets instantiated dynamically and isn't defined in a QML file instead it is defined an C++ file. Am I doing something wrong?
Header file
class MainController : public QObject { Q_OBJECT Q_PROPERTY(QCamera* cam READ getCamera() CONSTANT) public: explicit MainController(QObject *layout = nullptr , QObject *parent = nullptr); ~MainController(); Q_INVOKABLE QCamera* getCamera(); public slots: Q_INVOKABLE void buttonClicked(); };
Main
int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; MainController controller; engine.rootContext()->setContextProperty("controller" , &controller); engine.load(QUrl(QLatin1String("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
QML
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import QtMultimedia 5.8 Rectangle { id: rect height: 480 width: 640 color: "grey" anchors.centerIn: parent /* Camera { id: camera objectName: "camera" captureMode: Camera.CaptureViewfinder viewfinder.maximumFrameRate: 25.0 viewfinder.minimumFrameRate: 25.0 viewfinder.resolution: "640x480" } */ VideoOutput { id: viewFinder objectName: "viewFinder" anchors.fill: parent focus : visible source: controller.cam } }
I've tested it with the Camera defined in QML and that just works.
-
You can't just assign the source to a QCamera.
Bear in mind that QDeclarative:Camera is not a QCamera.
What you need to do is similar to this:
https://forum.qt.io/topic/27196/use-qml-videooutput-item-with-c-qmediaplayerPut your QCamera in the mediaplayer object and set the viewFinder to the surface.
I have some code that works in this manner.
-
@mranger90 Thanks!