I use Qt5.12.0 on windows 10, I want to see if it's possible to set a qml window into my rootObject qml application window and draw in front of this child window.
Here is my actually code.
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: "transparent"
}
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: "transparent"
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include "video.h"
#include <QQuickWindow>
#include <QUrl>
int main(int argc, char *argv[])
{
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QQuickWindow* window;
window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
CVideo *_pVideo = new CVideo();
_pVideo->init(&engine, window);
return app.exec();
}
#include <QApplication>
#include <QQmlApplicationEngine>
#include "video.h"
#include <QQuickWindow>
#include <QUrl>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QQuickWindow* window;
window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
CVideo *_pVideo = new CVideo();
_pVideo->init(&engine, window);
return app.exec();
}
To copy to clipboard, switch view to plain text mode
video.h
#ifndef VIDEO_H
#define VIDEO_H
#include <QQmlApplicationEngine>
#include <QQuickWindow>
class CVideo
{
public:
CVideo() = default;
void init(QQmlApplicationEngine* _engine, QQuickWindow* _pWindow);
};
#endif
#ifndef VIDEO_H
#define VIDEO_H
#include <QQmlApplicationEngine>
#include <QQuickWindow>
class CVideo
{
public:
CVideo() = default;
void init(QQmlApplicationEngine* _engine, QQuickWindow* _pWindow);
};
#endif
To copy to clipboard, switch view to plain text mode
video.cpp
#include "video.h"
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <QQmlComponent>
#include <QQuickItem>
#include <QQuickWindow>
void CVideo::init(QQmlApplicationEngine* _engine, QQuickWindow* _pWindow)
{
gst_init(nullptr, nullptr);
GstElement *pipeline =
gst_parse_launch
(
"rtspsrc location=rtsp://192.168.1.168:554/hdmi latency=50 ! application/x-rtp,media=(string)video ! decodebin ! glimagesink",
nullptr);
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
WId xwinid = -1;
QQmlComponent
*_pComponent
= new QQmlComponent
(_engine,
QUrl(QStringLiteral
("QML/VideoItem.qml")), _pWindow
);
QQuickItem* _pVideoItem = qobject_cast<QQuickItem*>(_pComponent->create());
_pVideoItem->setParent(_pWindow);
_pVideoItem->setParentItem(_pWindow->contentItem());
_pVideoItem->setProperty("id", "_videoOutput");
QObject *_pRootObject
= dynamic_cast<QObject
*>
(_engine
->rootObjects
().
first());
QList<QWindow*> _allWindow = _pRootObject->findChildren<QWindow*>();
QWindow *_window = _allWindow.first();
xwinid = _window->winId();
gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
_window->setParent(_pWindow);
GstStateChangeReturn sret = gst_element_set_state (pipeline,
GST_STATE_PLAYING);
if (sret == GST_STATE_CHANGE_FAILURE) {
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
}
}
#include "video.h"
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <QQmlComponent>
#include <QQuickItem>
#include <QQuickWindow>
void CVideo::init(QQmlApplicationEngine* _engine, QQuickWindow* _pWindow)
{
gst_init(nullptr, nullptr);
GstElement *pipeline =
gst_parse_launch
(
"rtspsrc location=rtsp://192.168.1.168:554/hdmi latency=50 ! application/x-rtp,media=(string)video ! decodebin ! glimagesink",
nullptr);
GstElement *sink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
WId xwinid = -1;
QQmlComponent *_pComponent = new QQmlComponent(_engine, QUrl(QStringLiteral("QML/VideoItem.qml")), _pWindow);
QQuickItem* _pVideoItem = qobject_cast<QQuickItem*>(_pComponent->create());
_pVideoItem->setParent(_pWindow);
_pVideoItem->setParentItem(_pWindow->contentItem());
_pVideoItem->setProperty("id", "_videoOutput");
QObject *_pRootObject = dynamic_cast<QObject*>(_engine->rootObjects().first());
QList<QWindow*> _allWindow = _pRootObject->findChildren<QWindow*>();
QWindow *_window = _allWindow.first();
xwinid = _window->winId();
gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (sink), xwinid);
_window->setParent(_pWindow);
GstStateChangeReturn sret = gst_element_set_state (pipeline,
GST_STATE_PLAYING);
if (sret == GST_STATE_CHANGE_FAILURE) {
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
}
}
To copy to clipboard, switch view to plain text mode
VideoItem.qml
import QtQuick 2.0
import QtQuick.Window 2.3
import QtQuick.Controls 2.4
Item{
id: _video
x: 0
y: 0
width: 1280
height: 800
visible: true
Window{
id: _videoOutput
visible: true
x: 100
y: 100
width: 800
height: 600
}
Button{
id: _button
x: 10
y: 10
width: 100
height: 100
text: "test"
}
}
import QtQuick 2.0
import QtQuick.Window 2.3
import QtQuick.Controls 2.4
Item{
id: _video
x: 0
y: 0
width: 1280
height: 800
visible: true
Window{
id: _videoOutput
visible: true
x: 100
y: 100
width: 800
height: 600
}
Button{
id: _button
x: 10
y: 10
width: 100
height: 100
text: "test"
}
}
To copy to clipboard, switch view to plain text mode
With this code I see my video read by gstreamer (1.14.4) but my button is under the video, so I want it in front of it.
It's possible to draw qml item (label, button, image,...) in front of my video item ? Thanks in advance for your response.
Bookmarks