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 | |
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')
26 files changed, 155 insertions, 367 deletions
diff --git a/examples/quick/quickwidgets/CMakeLists.txt b/examples/quick/quickwidgets/CMakeLists.txt index e0537e1e01..f205e23600 100644 --- a/examples/quick/quickwidgets/CMakeLists.txt +++ b/examples/quick/quickwidgets/CMakeLists.txt @@ -1,2 +1,2 @@ qt_internal_add_example(quickwidget) -qt_internal_add_example(qquickviewcomparison) +qt_internal_add_example(qquickwidgetversuswindow_opengl) diff --git a/examples/quick/quickwidgets/qquickviewcomparison/CMakeLists.txt b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/CMakeLists.txt index 2ac77c313c..c49bb915ec 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/CMakeLists.txt +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(qquickviewcomparison LANGUAGES CXX) +project(qquickwidgetversuswindow_opengl LANGUAGES CXX) set(CMAKE_AUTOMOC ON) @@ -7,23 +7,22 @@ if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/quickwidgets/qquickviewcomparison") +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/quickwidgets/qquickwidgetversuswindow_opengl") find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick QuickWidgets ShaderTools Widgets) -qt_add_executable(qquickviewcomparison +qt_add_executable(qquickwidgetversuswindow_opengl fbitem.cpp fbitem.h logo.cpp logo.h main.cpp mainwindow.cpp mainwindow.h ) -set_target_properties(qquickviewcomparison PROPERTIES +set_target_properties(qquickwidgetversuswindow_opengl PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) - -target_link_libraries(qquickviewcomparison PUBLIC +target_link_libraries(qquickwidgetversuswindow_opengl PUBLIC Qt::Core Qt::Gui Qt::Quick @@ -31,24 +30,24 @@ target_link_libraries(qquickviewcomparison PUBLIC Qt::Widgets ) -qt_add_qml_module(qquickviewcomparison +qt_add_qml_module(qquickwidgetversuswindow_opengl URI fbitem VERSION 1.0 QML_FILES test.qml - RESOURCE_PREFIX /qquickviewcomparison + RESOURCE_PREFIX /qquickwidgetversuswindow_opengl NO_RESOURCE_TARGET_PATH ) -qt6_add_shaders(qquickviewcomparison "shaders" +qt6_add_shaders(qquickwidgetversuswindow_opengl "shaders" PRECOMPILE OPTIMIZED PREFIX - "/qquickviewcomparison" + "/qquickwidgetversuswindow_opengl" FILES "wobble.frag" ) -install(TARGETS qquickviewcomparison +install(TARGETS qquickwidgetversuswindow_opengl RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/images/qquickwidgetversuswindow-opengl-example.jpg b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/images/qquickwidgetversuswindow-opengl-example.jpg Binary files differnew file mode 100644 index 0000000000..584ac4ed69 --- /dev/null +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/images/qquickwidgetversuswindow-opengl-example.jpg diff --git a/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/src/qquickwidgetversuswindow_opengl.qdoc b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/src/qquickwidgetversuswindow_opengl.qdoc new file mode 100644 index 0000000000..97ad7ddd8b --- /dev/null +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/doc/src/qquickwidgetversuswindow_opengl.qdoc @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** 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. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: https://www.gnu.org/licenses/fdl-1.3.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \title QQuickWidget - QQuickView Comparison Example + \example quickwidgets/qquickwidgetversuswindow_opengl + \brief Demonstrates the benefits of QQuickWidget over a QQuickView embedded as a native window. + \image qquickwidgetversuswindow-opengl-example.jpg + + The example allows showing the same Qt Quick scene within the same + QWidget-based user interface using two different approaches: one is using a + QQuickView and using it as a child window and + QWidget::createWindowContainer(). The other is using QQuickWidget which + acts as a true QWidget and supports, among others, better clipping and + stacking, as is expected from a true QWidget. + + The Qt Quick scene also uses direct OpenGL rendering, proving that with + QQuickWidget the rendering still happens using the accelerated 3D API. The + implication of this is that the example is tied to OpenGL and will not work + with other graphics APIs (Vulkan, Metal, Direct 3D). + + Clicking on the Show Overlay checkbox demonstrates a widget stacking case + that is not rendering as expected when embedding a native window, while + QQuickWidget makes it render as expected. + + Multisample antialiasing can also be exercised. When enabling it via the + corresponding checkbox, QQuickWidget will internally render into a + multisample renderbuffer, which is then resolved into a regular, + non-multisample 2D texture after each render pass, thus enabling + multisampling for the Qt Quick content without affecting the rest of the + QWidget UI. With QQuickView the native window will request multisampling + for its corresponding window surface, bringing similar results, if + supported by the platform. + + The Qt Quick scene is interactive: typing into the text field is possible + regardless of which embedding technology is used. QQuickWidget takes care + of forwarding input events to the underlying Qt Quick scene automatically. +*/ diff --git a/examples/quick/quickwidgets/qquickviewcomparison/fbitem.cpp b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/fbitem.cpp index 139fcbae5c..139fcbae5c 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/fbitem.cpp +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/fbitem.cpp diff --git a/examples/quick/quickwidgets/qquickviewcomparison/fbitem.h b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/fbitem.h index 573cfd7866..573cfd7866 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/fbitem.h +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/fbitem.h diff --git a/examples/quick/quickwidgets/qquickviewcomparison/logo.cpp b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/logo.cpp index b11c326034..b11c326034 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/logo.cpp +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/logo.cpp diff --git a/examples/quick/quickwidgets/qquickviewcomparison/logo.h b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/logo.h index 7e58bbb7b7..7e58bbb7b7 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/logo.h +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/logo.h diff --git a/examples/quick/quickwidgets/qquickviewcomparison/main.cpp b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/main.cpp index c0a26eefba..c0a26eefba 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/main.cpp +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/main.cpp diff --git a/examples/quick/quickwidgets/qquickviewcomparison/mainwindow.cpp b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/mainwindow.cpp index b393937809..27d320892e 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/mainwindow.cpp +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/mainwindow.cpp @@ -158,7 +158,7 @@ void MainWindow::updateView() ? m_currentRootObject->property("currentText").toString() : QStringLiteral("Hello Qt"); - QUrl source("qrc:qquickviewcomparison/test.qml"); + QUrl source("qrc:qquickwidgetversuswindow_opengl/test.qml"); if (m_state == UseWindow) { auto *quickView = new QQuickView; diff --git a/examples/quick/quickwidgets/qquickviewcomparison/mainwindow.h b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/mainwindow.h index d182b70960..d182b70960 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/mainwindow.h +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/mainwindow.h diff --git a/examples/quick/quickwidgets/qquickviewcomparison/qquickviewcomparison.pro b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/qquickwidgetversuswindow_opengl.pro index 7809c20b3a..42930b1ecd 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/qquickviewcomparison.pro +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/qquickwidgetversuswindow_opengl.pro @@ -1,5 +1,5 @@ TEMPLATE = app -TARGET = qquickviewcomparison +TARGET = qquickwidgetversuswindow_opengl CONFIG += qmltypes QML_IMPORT_NAME = fbitem @@ -16,9 +16,9 @@ HEADERS += mainwindow.h \ logo.h \ fbitem.h -RESOURCES += qquickviewcomparison.qrc +RESOURCES += qquickwidgetversuswindow_opengl.qrc OTHER_FILES += test.qml -target.path = $$[QT_INSTALL_EXAMPLES]/quick/quickwidgets/qquickviewcomparison +target.path = $$[QT_INSTALL_EXAMPLES]/quick/quickwidgets/qquickwidgetversuswindow_opengl INSTALLS += target diff --git a/examples/quick/quickwidgets/qquickviewcomparison/qquickviewcomparison.qrc b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/qquickwidgetversuswindow_opengl.qrc index e545c993c0..4650ef97e8 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/qquickviewcomparison.qrc +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/qquickwidgetversuswindow_opengl.qrc @@ -1,5 +1,5 @@ <RCC> - <qresource prefix="/qquickviewcomparison"> + <qresource prefix="/qquickwidgetversuswindow_opengl"> <file>test.qml</file> <file>wobble.frag.qsb</file> </qresource> diff --git a/examples/quick/quickwidgets/qquickviewcomparison/test.qml b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/test.qml index 8224320509..8224320509 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/test.qml +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/test.qml diff --git a/examples/quick/quickwidgets/qquickviewcomparison/wobble.frag b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/wobble.frag index 9fddf1a162..9fddf1a162 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/wobble.frag +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/wobble.frag diff --git a/examples/quick/quickwidgets/qquickviewcomparison/wobble.frag.qsb b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/wobble.frag.qsb Binary files differindex ab764e8326..ab764e8326 100644 --- a/examples/quick/quickwidgets/qquickviewcomparison/wobble.frag.qsb +++ b/examples/quick/quickwidgets/qquickwidgetversuswindow_opengl/wobble.frag.qsb 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 - } -} diff --git a/examples/quick/quickwidgets/quickwidgets.pro b/examples/quick/quickwidgets/quickwidgets.pro index be932f33d0..14d1893776 100644 --- a/examples/quick/quickwidgets/quickwidgets.pro +++ b/examples/quick/quickwidgets/quickwidgets.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs SUBDIRS = quickwidget \ - qquickviewcomparison + qquickwidgetversuswindow_opengl |