QML Basic Example#

Basic Qt tutorial.

from __future__ import annotations

import os

import numpy as np
from qtpy.QtGui import QColor
from qtpy.QtGui import QImage
from qtpy.QtQml import QQmlApplicationEngine
from qtpy.QtWidgets import QApplication

from qtvideo.constants import DEFAULT_IMAGE_FORMAT
from qtvideo.providers import FrameProvider


app = QApplication([])

os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
os.environ["QT_QUICK_CONTROLS_MATERIAL_THEME"] = "Dark"

Create a widget, populate it with a layout, then add a label with some text:

qml = b"""
import QtQuick
import QtQuick.Controls
import QtMultimedia

ApplicationWindow {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("QtGallery")
    VideoOutput {
        id: videoOutput
        anchors.fill: parent
        Component.onCompleted: frameProvider.videoSink = videoOutput.videoSink
    }
}
"""
engine = QQmlApplicationEngine()

frame_provider = FrameProvider()
engine.rootContext().setContextProperty("frameProvider", frame_provider)

engine.loadData(qml)

color = QColor("purple")
np_frame = np.zeros((100, 100, 4), dtype=np.uint8)
r, g, b, a = color.getRgb()
np_frame[:, :] = (b, g, r, a)
frame_provider.write_frame(np_frame)
qml example

Out:

True

Show the widget so it’ll render right after this text:

image = QImage(600, 600, DEFAULT_IMAGE_FORMAT)
image.fill(QColor("blue"))
frame_provider.write_frame(image)
qml example

Out:

True

Show the widget so it’ll render right after this text:

if __name__ == "__main__":
    app.exec()

Total running time of the script: ( 0 minutes 5.125 seconds)

Gallery generated by Sphinx-Gallery