Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Setting VideoOutput source programmatically
Qt 6.12 LTS is out! See what's new in the release blog

Setting VideoOutput source programmatically

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shiba
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mranger90M Offline
      mranger90M Offline
      mranger90
      wrote on last edited by
      #2

      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-qmediaplayer

      Put your QCamera in the mediaplayer object and set the viewFinder to the surface.

      I have some code that works in this manner.

      S 1 Reply Last reply
      1
      • mranger90M mranger90

        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-qmediaplayer

        Put your QCamera in the mediaplayer object and set the viewFinder to the surface.

        I have some code that works in this manner.

        S Offline
        S Offline
        Shiba
        wrote on last edited by
        #3

        @mranger90 Thanks!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved