diff options
Diffstat (limited to 'src/qml/doc/snippets/qmltc')
-rw-r--r-- | src/qml/doc/snippets/qmltc/CMakeLists.txt | 47 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/MyButton.qml | 77 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/MySlider.qml | 97 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/colorpicker.cpp | 76 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/colorpicker.h | 74 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/myApp.qml | 119 | ||||
-rw-r--r-- | src/qml/doc/snippets/qmltc/tst_qmltc_examples.cpp | 153 |
7 files changed, 643 insertions, 0 deletions
diff --git a/src/qml/doc/snippets/qmltc/CMakeLists.txt b/src/qml/doc/snippets/qmltc/CMakeLists.txt new file mode 100644 index 0000000000..d4ef2d0fc1 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/CMakeLists.txt @@ -0,0 +1,47 @@ +set(application_name tst_qmltc_examples) +#[[ +#! [qmltc-app-name] +# Use "my_qmltc_example" as an application name: +set(application_name my_qmltc_example) +#! [qmltc-app-name] +]] + +qt_internal_add_test(${application_name} + SOURCES + tst_qmltc_examples.cpp + colorpicker.h colorpicker.cpp + LIBRARIES + Qt::Core + Qt::Qml + Qt::Quick + Qt::QuickTemplates2Private # special + Qt::Gui +) + +#! [qmltc-add-qml-module] +# Create a CMake target, add C++ source files, link libraries, etc... + +# Make the application into a proper QML module: +qt6_add_qml_module(${application_name} + VERSION 1.0 + URI QmltcExample + QML_FILES + myApp.qml + MyButton.qml + MySlider.qml +) +#! [qmltc-add-qml-module] + +#! [qmltc-compile-to-cpp] +# (qmltc-specific) Link *private* libraries that correspond to QML modules: +target_link_libraries(${application_name} PRIVATE Qt::QmlPrivate Qt::QuickPrivate) + +# Compile qml files (listed in FILES) to C++ using qmltc and add these files to +# the application binary: +qt6_target_compile_qml_to_cpp(${application_name} + FILES + myApp.qml + MyButton.qml + MySlider.qml +) +#! [qmltc-compile-to-cpp] diff --git a/src/qml/doc/snippets/qmltc/MyButton.qml b/src/qml/doc/snippets/qmltc/MyButton.qml new file mode 100644 index 0000000000..ed7e177412 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/MyButton.qml @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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 + +Rectangle { + id: button + property alias text: textItem.text + signal clicked() + + readonly property color constantColor: "#63ACBE" + color: mouseArea.pressed ? Qt.lighter(constantColor) : constantColor + width: textItem.implicitWidth + 5 + height: textItem.implicitHeight + 5 + radius: 10 + + Text { + id: textItem + font.pixelSize: 22 + color: "black" + anchors.centerIn: button + } + + MouseArea { + id: mouseArea + anchors.fill: button + anchors.margins: -5 + onClicked: function(event) { button.clicked(); } + } +} diff --git a/src/qml/doc/snippets/qmltc/MySlider.qml b/src/qml/doc/snippets/qmltc/MySlider.qml new file mode 100644 index 0000000000..2687e10820 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/MySlider.qml @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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 +import QtQuick.Templates as T // we cannot use QQC2 (yet), but we can use its backend + +T.Slider { + id: control + + // QQC2-specific begin + implicitWidth: Math.max(control.implicitBackgroundWidth + control.leftInset + + control.rightInset, + control.implicitHandleWidth + control.leftPadding + + control.rightPadding) + implicitHeight: Math.max(control.implicitBackgroundHeight + control.topInset + + control.bottomInset, + control.implicitHandleHeight + control.topPadding + + control.bottomPadding) + padding: 6 + // QQC2-specific end + + background: Rectangle { + x: control.leftPadding + y: control.topPadding + control.availableHeight / 2 - height / 2 + implicitWidth: 200 + implicitHeight: 4 + width: control.availableWidth + height: implicitHeight + radius: 2 + border.color: "black" + color: "#F9F3EC" + + Rectangle { + width: control.visualPosition * parent.width + height: parent.height + color: "#63ACBE" + radius: 2 + } + } + + handle: Rectangle { + x: control.leftPadding + control.visualPosition * (control.availableWidth - width) + y: control.topPadding + control.availableHeight / 2 - height / 2 + implicitWidth: 26 + implicitHeight: 26 + radius: 13 + color: control.pressed ? Qt.lighter("#63ACBE") : "#63ACBE" + border.color: Qt.darker("#63ACBE") + } +} diff --git a/src/qml/doc/snippets/qmltc/colorpicker.cpp b/src/qml/doc/snippets/qmltc/colorpicker.cpp new file mode 100644 index 0000000000..fd678b0f8f --- /dev/null +++ b/src/qml/doc/snippets/qmltc/colorpicker.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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 "colorpicker.h" + +#include <QtCore/qlogging.h> + +void MyColorPicker::setEncodedColor(double value) +{ + if (value < 0.0 || !(value < 1.0)) { + qWarning("Bad value, %f, cannot get color from it!", value); + return; + } + m_encodedColor = value; +} + +QBindable<double> MyColorPicker::bindableEncodedColor() +{ + return QBindable<double>(&m_encodedColor); +} + +QColor MyColorPicker::decodeColor() +{ + const double encodedValue = m_encodedColor; + constexpr int rgbFirst = 0; + constexpr int rgbLast = 256 * 256 * 256; + const QRgb rgb = rgbFirst + (rgbLast - rgbFirst) * encodedValue; + return QColor(rgb); +} diff --git a/src/qml/doc/snippets/qmltc/colorpicker.h b/src/qml/doc/snippets/qmltc/colorpicker.h new file mode 100644 index 0000000000..275308fa66 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/colorpicker.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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 <QtCore/qobject.h> +#include <QtCore/qproperty.h> +#include <QtQml/qqmlregistration.h> +#include <QtGui/qcolor.h> + +class MyColorPicker : public QObject +{ + Q_OBJECT + QML_ELEMENT + // Stores a value in the range [0, 1); myApp.qml type sets this with + // Math.random() + Q_PROPERTY(double encodedColor READ encodedColor WRITE setEncodedColor BINDABLE bindableEncodedColor) + + QProperty<double> m_encodedColor{0.5}; +public: + MyColorPicker(QObject *parent = nullptr) : QObject(parent) {} + + double encodedColor() { return m_encodedColor; } + void setEncodedColor(double value); + QBindable<double> bindableEncodedColor(); + + // Returns a QColor "decoded" from encodedColor + Q_INVOKABLE QColor decodeColor(); +}; diff --git a/src/qml/doc/snippets/qmltc/myApp.qml b/src/qml/doc/snippets/qmltc/myApp.qml new file mode 100644 index 0000000000..234fa39ab9 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/myApp.qml @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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 +import QmltcExample 1.0 // application's own QML module + +Rectangle { + id: window + + width: 640 + height: 480 + focus: true + color: "#F9F4EC" + + readonly property color textColor: "#601A4A" + + Row { + id: row + anchors.centerIn: window + spacing: 10 + + Column { + id: column + spacing: 5 + + Text { + text: "Hello, QML World!" + font.pixelSize: slider.value + color: textColor + } + + MySlider { + id: slider + from: 20 + value: 20 + to: 30 + } + } + + Column { + spacing: 5 + + Text { + id: rndText + font.pixelSize: 25 + color: textColor + } + + Rectangle { + id: rndColorRect + height: 20 + width: rndButton.width + color: "black" + + MyColorPicker { // comes from C++ + id: colorPicker + onEncodedColorChanged: rndColorRect.color = colorPicker.decodeColor() + } + } + + MyButton { + id: rndButton + text: "PICK" + onClicked: function() { + var value = Math.random(); + rndText.text = value.toFixed(rndButton.text.length - 2); + colorPicker.encodedColor = value; + } + } + } + } +} diff --git a/src/qml/doc/snippets/qmltc/tst_qmltc_examples.cpp b/src/qml/doc/snippets/qmltc/tst_qmltc_examples.cpp new file mode 100644 index 0000000000..8de430de86 --- /dev/null +++ b/src/qml/doc/snippets/qmltc/tst_qmltc_examples.cpp @@ -0,0 +1,153 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the documentation 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$ +** +****************************************************************************/ + +// Note: this file is published under a license that is different from a default +// test sources license. This is intentional to comply with default +// snippet license. + +#include <QtGui/qguiapplication.h> +#include <QtCore/qtimer.h> +#include <QtTest/qtest.h> +#include <QtQuick/qquickwindow.h> +//! [qqmlcomponent-include] +#include <QtQml/qqmlcomponent.h> +//! [qqmlcomponent-include] + +//! [qmltc-include] +#include "myapp.h" // include generated C++ header +//! [qmltc-include] + +class tst_qmltc_examples : public QObject +{ + Q_OBJECT + + static constexpr int m_argc = 1; + static constexpr char *m_argv[] = { const_cast<char *>("tst_qmltc_examples") }; + +public: + tst_qmltc_examples(QObject *parent = nullptr) : QObject(parent) { } + +private slots: + void app(); + void appComponent(); +}; + +#define CREATE_DUMMY_ARGC_ARGV() \ + int argc = 1; \ + char *argv[] = { const_cast<char *>("tst_qmltc_examples") }; + +void tst_qmltc_examples::app() +{ + CREATE_DUMMY_ARGC_ARGV() + + //! [qmltc-app-code] + QGuiApplication app(argc, argv); + app.setApplicationDisplayName(QStringLiteral("This example is powered by qmltc!")); + + QQmlEngine e; + QQuickWindow window; + + QScopedPointer<myApp> documentRoot(new myApp(&e)); + + documentRoot->setParentItem(window.contentItem()); + window.setHeight(documentRoot->height()); + window.setWidth(documentRoot->width()); + // ... + //! [qmltc-app-code] + + QTimer::singleShot(1000, &app, QGuiApplication::quit); + + //! [qmltc-app-exec] + window.show(); + app.exec(); + //! [qmltc-app-exec] +} + +void tst_qmltc_examples::appComponent() +{ + CREATE_DUMMY_ARGC_ARGV() + + //! [qqmlcomponent-app-code-0] + QGuiApplication app(argc, argv); + app.setApplicationDisplayName(QStringLiteral("This example is powered by QQmlComponent :(")); + + QQmlEngine e; + QQuickWindow window; + + QQmlComponent component(&e); + component.loadUrl(QUrl(QStringLiteral("qrc:/QmltcExample/myApp.qml"))); + //! [qqmlcomponent-app-code-0] + + QVERIFY2(!component.isError(), qPrintable(component.errorString())); + + //! [qqmlcomponent-app-code-1] + QScopedPointer<QObject> documentRoot(component.create()); + QQuickItem *documentRootItem = qobject_cast<QQuickItem *>(documentRoot.get()); + //! [qqmlcomponent-app-code-1] + + QVERIFY(documentRootItem); + + //! [qqmlcomponent-app-code-2] + documentRootItem->setParentItem(window.contentItem()); + window.setHeight(documentRootItem->height()); + window.setWidth(documentRootItem->width()); + // ... + //! [qqmlcomponent-app-code-2] + + QTimer::singleShot(1000, &app, QGuiApplication::quit); + + window.show(); + app.exec(); +} + +#undef CREATE_DUMMY_ARGC_ARGV + +QTEST_APPLESS_MAIN(tst_qmltc_examples) +#include "tst_qmltc_examples.moc" |