<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qmlvideo.qdoc --> <title>QML Video Example | Qt Multimedia 5.12.2</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtmultimedia-index.html">Qt Multimedia</a></td><td ><a href="multimedia-examples.html">Qt Multimedia Examples</a></td><td >QML Video Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtmultimedia-index.html">Qt 5.12.2 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#application-structure">Application Structure</a></li> <li class="level1"><a href="#calculating-and-displaying-qml-painting-rate">Calculating and Displaying QML Painting Rate</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QML Video Example</h1> <span class="subtitle"></span> <!-- $$$multimedia/video/qmlvideo-brief --> <p>Transforming video and camera viewfinder content.</p> <!-- @@@multimedia/video/qmlvideo --> <!-- $$$multimedia/video/qmlvideo-description --> <div class="descr"> <a name="details"></a> <p><i>QML Video</i> demonstrates the various transformations (move; resize; rotate; change aspect ratio) that can be applied to QML <a href="qml-qtmultimedia-videooutput.html">VideoOutput</a> and <a href="qml-multimedia.html#camera">Camera</a> types.</p> <p>It also shows how native code can be combined with QML to implement more advanced functionality - in this case, C++ code is used to calculate the QML frame rate. This value is rendered in QML in a semi-transparent item overlaid on the video content.</p> <p>The following image shows the application executing the video-overlay scene, which creates a dummy overlay item (just a semi-transparent Rectangle), which moves across the <a href="qml-qtmultimedia-videooutput.html">VideoOutput</a> item.</p> <p class="centerAlign"><img src="images/qmlvideo-overlay.jpg" alt="" /></p><a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="application-structure"></a> <h2 id="application-structure">Application Structure</h2> <p>The <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-main-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/main.qml</a> file creates a UI which includes the following items:</p> <ul> <li>Two <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-button-qml.html">Button</a> instances, each of which displays a filename, and can be used to launch a <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-filebrowser-qml.html">FileBrowser</a></li> <li>An exit <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-button-qml.html">Button</a></li> <li>A <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-sceneselectionpanel-qml.html">SceneSelectionPanel</a>, which is a flickable list displaying the available scenes</li> <li>At the lower left, an item which displays the QML repainting rate - the upper number is the instantaneous frame rate and the lower number is the average over the past second.</li> </ul> <p class="centerAlign"><img src="images/qmlvideo-menu.jpg" alt="" /></p><p>Each scene in the flickable list is implemented in its own QML file - for example the video-basic scene (which just displays a static <a href="qml-qtmultimedia-videooutput.html">VideoOutput</a> in the center of the screen) is implemented in the <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videobasic-qml.html">VideoBasic.qml</a> file. As you can see from the code, this makes use of a type of inheritance: a <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videobasic-qml.html">VideoBasic</a> item ...</p> <pre class="cpp"> import QtQuick 2.0 SceneBasic { contentType: "video" } </pre> <p>... is-a <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenebasic-qml.html">SceneBasic</a> ...</p> <pre class="cpp"> import QtQuick 2.0 Scene { id: root property string contentType ... Content { id: content ... } Text { anchors { horizontalCenter: parent.horizontalCenter bottom: parent.bottom margins: 20 } text: content.started ? "Tap the screen to stop content" : "Tap the screen to start content" color: "#e0e0e0" z: 2.0 } MouseArea { anchors.fill: parent onClicked: { if (content.started) content.stop() else content.start() } } Component.onCompleted: root.content = content } </pre> <p>... which is-a <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scene-qml.html">Scene</a>:</p> <pre class="cpp"> import QtQuick 2.0 Rectangle { id: root ... property QtObject content ... Button { id: closeButton anchors { top: parent.top right: parent.right margins: root.margins } width: Math.max(parent.width, parent.height) / 12 height: Math.min(parent.width, parent.height) / 12 z: 2.0 bgColor: "#212121" bgColorSelected: "#757575" textColorSelected: "white" text: "Back" onClicked: root.close() } } </pre> <p><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenebasic-qml.html">SceneBasic</a> describes the structure and behaviour of the scene, but is agnostic of the type of content which will be displayed - this is abstracted by <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-content-qml.html">Content</a>.</p> <p>This pattern allows us to define a particular use case (in this case, simply display a static piece of content), and then instantiate that use case for both video content (<a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videobasic-qml.html">VideoBasic</a>) and camera content (<a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-camerabasic-qml.html">CameraBasic</a>). This approach is used to implement many of the other scenes - for example, "repeatedly slide the content from left to right and back again" is implemented by <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenemove-qml.html">SceneMove</a>, on which <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videomove-qml.html">VideoMove</a> and <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameramove-qml.html">CameraMove</a> are based.</p> <p>Depending on the value of the contentType property in the top-level scene instance, the embedded <a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-content-qml.html">Content</a> item creates either a <a href="qml-qtmultimedia-mediaplayer.html">MediaPlayer</a> or a <a href="qml-multimedia.html#camera">Camera</a> item.</p> <a name="calculating-and-displaying-qml-painting-rate"></a> <h2 id="calculating-and-displaying-qml-painting-rate">Calculating and Displaying QML Painting Rate</h2> <p>The QML painting rate is calculated by the FrequencyMonitor class, which turns a stream of events (received via the notify() slot), into an instantaneous and an averaged frequency:</p> <pre class="cpp"> <span class="keyword">class</span> FrequencyMonitor : <span class="keyword">public</span> <span class="type">QObject</span> { Q_OBJECT Q_PROPERTY(<span class="type">qreal</span> instantaneousFrequency READ instantaneousFrequency NOTIFY instantaneousFrequencyChanged) Q_PROPERTY(<span class="type">qreal</span> averageFrequency READ averageFrequency NOTIFY averageFrequencyChanged) <span class="keyword">public</span>: ... <span class="keyword">static</span> <span class="type">void</span> qmlRegisterType(); <span class="keyword">public</span> <span class="keyword">slots</span>: Q_INVOKABLE <span class="type">void</span> notify(); }; </pre> <p>The FrequencyMonitor class is exposed to QML like this</p> <pre class="cpp"> <span class="type">void</span> FrequencyMonitor<span class="operator">::</span>qmlRegisterType() { <span class="operator">::</span>qmlRegisterType<span class="operator"><</span>FrequencyMonitor<span class="operator">></span>(<span class="string">"FrequencyMonitor"</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"FrequencyMonitor"</span>); } </pre> <p>and its data is displayed by defining a QML item called FrequencyItem, like this:</p> <pre class="cpp"> import FrequencyMonitor 1.0 Rectangle { id: root ... function notify() { monitor.notify() } FrequencyMonitor { id: monitor onAverageFrequencyChanged: { averageFrequencyText.text = monitor.averageFrequency.toFixed(2) } } Text { id: labelText anchors { left: parent.left top: parent.top margins: 10 } color: root.textColor font.pixelSize: 0.6 * root.textSize text: root.label width: root.width - 2*anchors.margins elide: Text.ElideRight } Text { id: averageFrequencyText anchors { right: parent.right bottom: parent.bottom margins: 10 } color: root.textColor font.pixelSize: root.textSize } } </pre> <p>The result looks like this:</p> <p class="centerAlign"><img src="images/video-qml-paint-rate.png" alt="" /></p><p>All that remains is to connect the afterRendering() signal of the QQuickView object to a JavaScript function, which will eventually call frequencyItem.notify():</p> <pre class="cpp"> <span class="preprocessor">#include <QtGui/QGuiApplication></span> <span class="preprocessor">#include <QtQuick/QQuickItem></span> <span class="preprocessor">#include <QtQuick/QQuickView></span> <span class="preprocessor">#include "trace.h"</span> <span class="preprocessor">#ifdef PERFORMANCEMONITOR_SUPPORT</span> <span class="preprocessor">#include "performancemonitordeclarative.h"</span> <span class="preprocessor">#endif</span> <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">QString</span> DefaultFileName1 <span class="operator">=</span> <span class="string">""</span>; ... <span class="type">QQuickItem</span> <span class="operator">*</span>rootObject <span class="operator">=</span> viewer<span class="operator">.</span>rootObject(); ... <span class="type">QObject</span><span class="operator">::</span>connect(<span class="operator">&</span>viewer<span class="operator">,</span> SIGNAL(afterRendering())<span class="operator">,</span> rootObject<span class="operator">,</span> SLOT(qmlFramePainted())); </pre> <p>Files:</p> <ul> <li><a href="qtmultimedia-multimedia-video-qmlvideo-main-cpp.html">multimedia/video/qmlvideo/main.cpp</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-button-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/Button.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-camerabasic-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraBasic.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameradrag-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraDrag.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameradummy-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraDummy.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-camerafullscreen-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraFullScreen.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-camerafullscreeninverted-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraFullScreenInverted.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameraitem-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraItem.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameramove-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraMove.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameraoverlay-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraOverlay.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameraresize-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraResize.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-camerarotate-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraRotate.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-cameraspin-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/CameraSpin.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-content-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/Content.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-errordialog-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/ErrorDialog.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-filebrowser-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/FileBrowser.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scene-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/Scene.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenebasic-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneBasic.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenedrag-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneDrag.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenefullscreen-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneFullScreen.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenefullscreeninverted-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneFullScreenInverted.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenemove-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneMove.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenemulti-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneMulti.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-sceneoverlay-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneOverlay.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-sceneresize-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneResize.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenerotate-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneRotate.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-sceneselectionpanel-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneSelectionPanel.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-scenespin-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SceneSpin.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-seekcontrol-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/SeekControl.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videobasic-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoBasic.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videodrag-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoDrag.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videodummy-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoDummy.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videofillmode-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoFillMode.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videofullscreen-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoFullScreen.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videofullscreeninverted-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoFullScreenInverted.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videoitem-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoItem.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videometadata-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoMetadata.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videomove-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoMove.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videooverlay-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoOverlay.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videoplaybackrate-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoPlaybackRate.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videoresize-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoResize.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videorotate-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoRotate.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videoseek-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoSeek.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-videospin-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/VideoSpin.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qml-qmlvideo-main-qml.html">multimedia/video/qmlvideo/qml/qmlvideo/main.qml</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qmlvideo-pro.html">multimedia/video/qmlvideo/qmlvideo.pro</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qmlvideo-qrc.html">multimedia/video/qmlvideo/qmlvideo.qrc</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-qmlvideo-svg.html">multimedia/video/qmlvideo/qmlvideo.svg</a></li> <li><a href="qtmultimedia-multimedia-video-qmlvideo-trace-h.html">multimedia/video/qmlvideo/trace.h</a></li> </ul> <p>Images:</p> <ul> <li><a href="images/used-in-examples/multimedia/video/qmlvideo/images/folder.png">multimedia/video/qmlvideo/images/folder.png</a></li> <li><a href="images/used-in-examples/multimedia/video/qmlvideo/images/leaves.jpg">multimedia/video/qmlvideo/images/leaves.jpg</a></li> <li><a href="images/used-in-examples/multimedia/video/qmlvideo/images/up.png">multimedia/video/qmlvideo/images/up.png</a></li> <li><a href="images/used-in-examples/multimedia/video/qmlvideo/qmlvideo.png">multimedia/video/qmlvideo/qmlvideo.png</a></li> </ul> </div> <!-- @@@multimedia/video/qmlvideo --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>