diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2021-11-08 16:16:16 +0100 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2022-03-18 22:56:54 +0100 |
commit | a184f2e0abfa0daf9190b72901208373dd5c1f79 (patch) | |
tree | 915f42c5debae2c7627e45621f1462c05eab0308 /examples/quick/quickwidgets/quickwidget | |
parent | 40b6bcfb1b6297f36659b36d393c9a33403d8360 (diff) |
Make QQuickWidget QRhi-based
When it comes to examples, quickwidget has the direct OpenGL usage
removed because we want this example to be working with any graphics
API. qquickviewcomparison, which features direct OpenGL usage within
its Qt Quick scene, is renamed to a more descriptive name and is also
getting a doc landing page. It continues to be requesting OpenGL (via
QRhi) explicitly.
Change-Id: Iae5b835441f3af229e9746e14dedbe9d1a62b2b9
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'examples/quick/quickwidgets/quickwidget')
9 files changed, 76 insertions, 349 deletions
diff --git a/examples/quick/quickwidgets/quickwidget/CMakeLists.txt b/examples/quick/quickwidgets/quickwidget/CMakeLists.txt index f3cd7688c3..c8121d81a4 100644 --- a/examples/quick/quickwidgets/quickwidget/CMakeLists.txt +++ b/examples/quick/quickwidgets/quickwidget/CMakeLists.txt @@ -12,7 +12,6 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/quickwidgets/quickwidget") find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick QuickWidgets Widgets) qt_add_executable(quickwidget - fbitem.cpp fbitem.h main.cpp ) @@ -33,9 +32,7 @@ qt_add_qml_module(quickwidget URI QuickWidgetExample VERSION 1.0 QML_FILES - customgl.qml rotatingsquare.qml - rotatingsquaretab.qml RESOURCE_PREFIX /quickwidget NO_RESOURCE_TARGET_PATH ) diff --git a/examples/quick/quickwidgets/quickwidget/customgl.qml b/examples/quick/quickwidgets/quickwidget/customgl.qml deleted file mode 100644 index 577f006bd7..0000000000 --- a/examples/quick/quickwidgets/quickwidget/customgl.qml +++ /dev/null @@ -1,69 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -import QtQuick 2.0 -import QuickWidgetExample 1.0 - -Rectangle { - color: "lightGray" - - FbItem { - anchors.fill: parent - anchors.margins: 10 - } - - Text { - anchors.left: parent.left - anchors.bottom: parent.bottom - anchors.margins: 15 - text: "QQuickFramebufferObject with animated clear color" - color: "white" - } -} diff --git a/examples/quick/quickwidgets/quickwidget/fbitem.cpp b/examples/quick/quickwidgets/quickwidget/fbitem.cpp deleted file mode 100644 index 3e98168dcf..0000000000 --- a/examples/quick/quickwidgets/quickwidget/fbitem.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "fbitem.h" -#include <QOpenGLFramebufferObject> -#include <QOpenGLContext> -#include <QOpenGLFunctions> -#include <QtCore/QDebug> - -#if QT_CONFIG(opengl) -class FbRenderer : public QQuickFramebufferObject::Renderer -{ -public: - FbRenderer() { } - - // The lifetime of the FBO and this class depends on how QQuickWidget - // manages the scenegraph and context when it comes to showing and hiding - // the widget. The actual behavior is proven by the debug prints. - ~FbRenderer() override { - qDebug("FbRenderer destroyed"); - } - - void render() override { - QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); - f->glClearColor(c, 0, 0, 1); - f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - c += 0.01f * dir; - if (c >= 1.0f || c <= 0.0f) - dir *= -1; - update(); - } - - QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) override { - qDebug() << "Creating FBO" << size; - QOpenGLFramebufferObjectFormat format; - format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); - return new QOpenGLFramebufferObject(size, format); - } - -private: - float c = 0; - int dir = 1; -}; -#endif - -QQuickFramebufferObject::Renderer *FbItem::createRenderer() const -{ -#if QT_CONFIG(opengl) - return new FbRenderer; -#else - return nullptr; -#endif -} diff --git a/examples/quick/quickwidgets/quickwidget/fbitem.h b/examples/quick/quickwidgets/quickwidget/fbitem.h deleted file mode 100644 index eb5ec1f1d0..0000000000 --- a/examples/quick/quickwidgets/quickwidget/fbitem.h +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef FBITEM_H -#define FBITEM_H - -#include <QtQuick/QQuickFramebufferObject> - -class FbItem : public QQuickFramebufferObject -{ - Q_OBJECT - QML_ELEMENT -public: - Renderer *createRenderer() const override; -}; - -#endif diff --git a/examples/quick/quickwidgets/quickwidget/main.cpp b/examples/quick/quickwidgets/quickwidget/main.cpp index 191adcc451..5d1bc38aa7 100644 --- a/examples/quick/quickwidgets/quickwidget/main.cpp +++ b/examples/quick/quickwidgets/quickwidget/main.cpp @@ -48,14 +48,18 @@ ** ****************************************************************************/ +#include <QApplication> +#include <QCommandLineParser> #include <QQuickWidget> #include <QQuickItem> #include <QQmlError> -#include <QtWidgets> -#include "fbitem.h" - -static bool optMultipleSample = false; -static bool optCoreProfile = false; +#include <QMdiArea> +#include <QLCDNumber> +#include <QFileDialog> +#include <QStatusBar> +#include <QMainWindow> +#include <QMenuBar> +#include <QPushButton> class MainWindow : public QMainWindow { Q_OBJECT @@ -74,14 +78,12 @@ private: QQuickWidget *m_quickWidget; }; +static bool optMultipleSample = false; + MainWindow::MainWindow() : m_quickWidget(new QQuickWidget) { QSurfaceFormat format; - if (optCoreProfile) { - format.setVersion(4, 4); - format.setProfile(QSurfaceFormat::CoreProfile); - } if (optMultipleSample) format.setSamples(4); m_quickWidget->setFormat(format); @@ -120,23 +122,45 @@ MainWindow::MainWindow() void MainWindow::createQuickWidgetsInTabs(QMdiArea *mdiArea) { - QTabWidget *tabWidget = new QTabWidget; + // A QQuickWidget should work like any other widget when it comes to being + // in layouts, in tab widgets, MDI areas, etc. It can also be freely + // reparented and made top-level. + QTabWidget *tabWidget = new QTabWidget; const QSize size(400, 400); - - QQuickWidget *w = new QQuickWidget; - w->resize(size); - w->setResizeMode(QQuickWidget::SizeRootObjectToView); - w->setSource(QUrl("qrc:quickwidget/rotatingsquaretab.qml")); - - tabWidget->addTab(w, tr("Plain Quick content")); - - w = new QQuickWidget; - w->resize(size); - w->setResizeMode(QQuickWidget::SizeRootObjectToView); - w->setSource(QUrl("qrc:quickwidget/customgl.qml")); - - tabWidget->addTab(w, tr("Custom OpenGL drawing")); + const QString msgToTopLevel = QLatin1String("Break out to top-level window"); + const QString msgFromTopLevel = QLatin1String("Move back under tab widget"); + + static const int N = 4; + static const QColor colorTab[N] = { Qt::green, Qt::blue, Qt::yellow, Qt::magenta }; + for (int i = 0; i < N; ++i) { + QQuickWidget *widget = new QQuickWidget; + widget->resize(size); + widget->setResizeMode(QQuickWidget::SizeRootObjectToView); + QObject::connect(widget, &QQuickWidget::statusChanged, widget, [widget, i] { + if (widget->status() == QQuickWidget::Ready) { + if (QQuickItem *rootItem = widget->rootObject()) { + rootItem->setProperty("rectColor", colorTab[i]); + } + } + }); + widget->setSource(QUrl("qrc:quickwidget/rotatingsquare.qml")); + widget->setWindowTitle(QString::asprintf("Tab %d", i + 1)); + QPushButton *btn = new QPushButton(msgToTopLevel, widget); + connect(btn, &QPushButton::clicked, widget, [=] { + if (widget->parent()) { + widget->setAttribute(Qt::WA_DeleteOnClose, true); + widget->setParent(nullptr); + widget->show(); + btn->setText(msgFromTopLevel); + } else { + widget->setAttribute(Qt::WA_DeleteOnClose, false); + tabWidget->addTab(widget, widget->windowTitle()); + btn->setText(msgToTopLevel); + } + }); + tabWidget->addTab(widget, QString::asprintf("Tab %d", i + 1)); + } mdiArea->addSubWindow(tabWidget); tabWidget->show(); @@ -197,25 +221,20 @@ int main(int argc, char **argv) { QApplication app(argc, argv); - // this example and QQuickWidget are only functional when rendering with OpenGL - QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL); - QCoreApplication::setApplicationName("Qt QQuickWidget Example"); QCoreApplication::setOrganizationName("QtProject"); QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addHelpOption(); parser.addVersionOption(); QCommandLineOption multipleSampleOption("multisample", "Multisampling"); parser.addOption(multipleSampleOption); - QCommandLineOption coreProfileOption("coreprofile", "Use core profile"); - parser.addOption(coreProfileOption); parser.process(app); optMultipleSample = parser.isSet(multipleSampleOption); - optCoreProfile = parser.isSet(coreProfileOption); MainWindow mainWindow; mainWindow.show(); diff --git a/examples/quick/quickwidgets/quickwidget/quickwidget.pro b/examples/quick/quickwidgets/quickwidget/quickwidget.pro index b824c520f5..9c31ac28f3 100644 --- a/examples/quick/quickwidgets/quickwidget/quickwidget.pro +++ b/examples/quick/quickwidgets/quickwidget/quickwidget.pro @@ -7,8 +7,7 @@ CONFIG += qmltypes QML_IMPORT_NAME = QuickWidgetExample QML_IMPORT_MAJOR_VERSION = 1 -SOURCES += main.cpp fbitem.cpp -HEADERS += fbitem.h +SOURCES += main.cpp RESOURCES += quickwidget.qrc diff --git a/examples/quick/quickwidgets/quickwidget/quickwidget.qrc b/examples/quick/quickwidgets/quickwidget/quickwidget.qrc index 85a49b75ca..c073b7b80d 100644 --- a/examples/quick/quickwidgets/quickwidget/quickwidget.qrc +++ b/examples/quick/quickwidgets/quickwidget/quickwidget.qrc @@ -1,7 +1,5 @@ <RCC> <qresource prefix="/quickwidget"> <file>rotatingsquare.qml</file> - <file>rotatingsquaretab.qml</file> - <file>customgl.qml</file> </qresource> </RCC> diff --git a/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml b/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml index 5d3bb72fbe..08b769d80f 100644 --- a/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml +++ b/examples/quick/quickwidgets/quickwidget/rotatingsquare.qml @@ -48,10 +48,11 @@ ** ****************************************************************************/ -import QtQuick 2.0 +import QtQuick 2.8 Rectangle { id: root + property color rectColor: "red" Rectangle { property int d: 100 @@ -59,13 +60,35 @@ Rectangle { width: d height: d anchors.centerIn: parent - color: "red" + color: root.rectColor NumberAnimation on rotation { from: 0; to: 360; duration: 2000; loops: Animation.Infinite; } } Text { + id: text anchors.centerIn: parent - text: "Qt Quick running in a widget" + property string api + Connections { + target: text.GraphicsInfo + function onApiChanged() { + var api = text.GraphicsInfo.api; + if (api === GraphicsInfo.Software) + text.api = "Software"; + else if (api === GraphicsInfo.OpenGL) + text.api = "OpenGL on QRhi"; + else if (api === GraphicsInfo.Direct3D11) + text.api = "D3D11 on QRhi"; + else if (api === GraphicsInfo.Vulkan) + text.api = "Vulkan on QRhi"; + else if (api === GraphicsInfo.Metal) + text.api = "Metal on QRhi"; + else if (api === GraphicsInfo.Null) + text.api = "Null on QRhi"; + else + text.api = "Unknown API"; + } + } + text: "Qt Quick running in a widget\nGraphicsInfo.api says: " + api } function performLayerBasedGrab(fn) { diff --git a/examples/quick/quickwidgets/quickwidget/rotatingsquaretab.qml b/examples/quick/quickwidgets/quickwidget/rotatingsquaretab.qml deleted file mode 100644 index 116a8f8729..0000000000 --- a/examples/quick/quickwidgets/quickwidget/rotatingsquaretab.qml +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -import QtQuick 2.0 - -Rectangle { - gradient: Gradient { - GradientStop { position: 0; color: "steelblue" } - GradientStop { position: 1; color: "black" } - } - - Rectangle { - property int d: 100 - id: square - width: d - height: d - anchors.centerIn: parent - color: "green" - NumberAnimation on rotation { from: 360; to: 0; duration: 4000; loops: Animation.Infinite; } - } - - Text { - anchors.centerIn: parent - text: "Qt Quick running in a tab widget" - color: "purple" - font.bold: true - font.pointSize: 14 - } -} |