diff options
Diffstat (limited to 'tests/manual/qmlextensionplugins')
15 files changed, 369 insertions, 0 deletions
diff --git a/tests/manual/qmlextensionplugins/CMakeLists.txt b/tests/manual/qmlextensionplugins/CMakeLists.txt new file mode 100644 index 0000000000..26e454b0b4 --- /dev/null +++ b/tests/manual/qmlextensionplugins/CMakeLists.txt @@ -0,0 +1,62 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(qmlqtimeexample LANGUAGES CXX) + +set(CMAKE_AUTOMOC ON) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qml/qmlextensionplugins/imports/TimeExample") + +find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml) + +qt_standard_project_setup(REQUIRES 6.5) + +#![0] +set(qml_files + imports/TimeExample/Clock.qml +) + +set(images + imports/TimeExample/center.png + imports/TimeExample/clock.png + imports/TimeExample/hour.png + imports/TimeExample/minute.png +) +#![0] + +foreach(file IN LISTS qml_files images) + get_filename_component(filename ${file} NAME) + set_source_files_properties(${file} PROPERTIES QT_RESOURCE_ALIAS ${filename}) +endforeach() + +#![1] +qt_add_qml_module(qmlqtimeexample + OUTPUT_DIRECTORY imports/TimeExample + URI "TimeExample" + SOURCES timemodel.cpp timemodel.h + QML_FILES ${qml_files} + RESOURCES ${images} +) +#![1] + +set_target_properties(qmlqtimeexample PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(qmlqtimeexample PUBLIC + Qt6::Core + Qt6::Gui + Qt6::Qml +) + +install(TARGETS qmlqtimeexample + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/tests/manual/qmlextensionplugins/doc/images/qml-plugins-example.png b/tests/manual/qmlextensionplugins/doc/images/qml-plugins-example.png Binary files differnew file mode 100644 index 0000000000..c2d4886ea2 --- /dev/null +++ b/tests/manual/qmlextensionplugins/doc/images/qml-plugins-example.png diff --git a/tests/manual/qmlextensionplugins/doc/src/qmlpluginex.qdoc b/tests/manual/qmlextensionplugins/doc/src/qmlpluginex.qdoc new file mode 100644 index 0000000000..80f39e36ce --- /dev/null +++ b/tests/manual/qmlextensionplugins/doc/src/qmlpluginex.qdoc @@ -0,0 +1,43 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only + +/*! + \title QML Plugin Example + \example qmlextensionplugins + + \brief This example creates a C++ plugin extension by subclassing QQmlEngineExtensionPlugin. + + \image qml-plugins-example.png + + To run this example, use the following commands on the prompt: + + \if defined(onlinedocs) + \tab {build-qt-app}{tab-cmake}{CMake}{checked} + \tab {build-qt-app}{tab-qmake}{qmake}{} + \tabcontent {tab-cmake} + \else + \section1 Using CMake + \endif + \badcode \QtVersion + cmake -DCMAKE_PREFIX_PATH=path/to/Qt/\1/your_platform -S <source-dir> -B <build-dir> -G Ninja + cd path/to/build/dir + ninja + qml -I imports plugins.qml + \endcode + For more information about building on the command line using CMake, + see \l {Building projects on the command line}. + \if defined(onlinedocs) + \endtabcontent + \tabcontent {tab-qmake} + \else + \section1 Using qmake + \endif + \badcode + qmake + make + qml -I imports plugins.qml + \endcode + \if defined(onlinedocs) + \endtabcontent + \endif +*/ diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/Clock.qml b/tests/manual/qmlextensionplugins/imports/TimeExample/Clock.qml new file mode 100644 index 0000000000..416b78d31c --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/Clock.qml @@ -0,0 +1,51 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import QtQuick + +Rectangle { + id: clock + width: 200; height: 200; color: "gray" + + property alias city: cityLabel.text + property variant hours + property variant minutes + property variant shift : 0 + + Image { id: background; source: "clock.png" } + + Image { + x: 92.5; y: 27 + source: "hour.png" + transform: Rotation { + id: hourRotation + origin.x: 7.5; origin.y: 73; + angle: (clock.hours * 30) + (clock.minutes * 0.5) + Behavior on angle { + SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 } + } + } + } + + Image { + x: 93.5; y: 17 + source: "minute.png" + transform: Rotation { + id: minuteRotation + origin.x: 6.5; origin.y: 83; + angle: clock.minutes * 6 + Behavior on angle { + SpringAnimation{ spring: 2; damping: 0.2; modulus: 360 } + } + } + } + + Image { + anchors.centerIn: background; source: "center.png" + } + + Text { + id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white" + anchors.horizontalCenter: parent.horizontalCenter + } +} diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/center.png b/tests/manual/qmlextensionplugins/imports/TimeExample/center.png Binary files differnew file mode 100644 index 0000000000..7fbd802a44 --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/center.png diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/clock.png b/tests/manual/qmlextensionplugins/imports/TimeExample/clock.png Binary files differnew file mode 100644 index 0000000000..462edacc0e --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/clock.png diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/hour.png b/tests/manual/qmlextensionplugins/imports/TimeExample/hour.png Binary files differnew file mode 100644 index 0000000000..9f33fc5d48 --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/hour.png diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/minute.png b/tests/manual/qmlextensionplugins/imports/TimeExample/minute.png Binary files differnew file mode 100644 index 0000000000..e2f216c897 --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/minute.png diff --git a/tests/manual/qmlextensionplugins/imports/TimeExample/qmldir b/tests/manual/qmlextensionplugins/imports/TimeExample/qmldir new file mode 100644 index 0000000000..252e662e12 --- /dev/null +++ b/tests/manual/qmlextensionplugins/imports/TimeExample/qmldir @@ -0,0 +1,3 @@ +module TimeExample +Clock 1.0 Clock.qml +plugin qmlqtimeexampleplugin diff --git a/tests/manual/qmlextensionplugins/plugin.cpp b/tests/manual/qmlextensionplugins/plugin.cpp new file mode 100644 index 0000000000..afda6626ce --- /dev/null +++ b/tests/manual/qmlextensionplugins/plugin.cpp @@ -0,0 +1,15 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtQml/QQmlEngineExtensionPlugin> +#include <qdebug.h> + +//![plugin] +class QExampleQmlPlugin : public QQmlEngineExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid) +}; +//![plugin] + +#include "plugin.moc" diff --git a/tests/manual/qmlextensionplugins/plugins.qml b/tests/manual/qmlextensionplugins/plugins.qml new file mode 100644 index 0000000000..9e798db7e2 --- /dev/null +++ b/tests/manual/qmlextensionplugins/plugins.qml @@ -0,0 +1,16 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only +//![0] +import TimeExample // import types from the plugin + +Clock { // this class is defined in QML (imports/TimeExample/Clock.qml) + + Time { // this class is defined in C++ (plugin.cpp) + id: time + } + + hours: time.hour + minutes: time.minute + +} +//![0] diff --git a/tests/manual/qmlextensionplugins/plugins.qmlproject b/tests/manual/qmlextensionplugins/plugins.qmlproject new file mode 100644 index 0000000000..c2de5f5703 --- /dev/null +++ b/tests/manual/qmlextensionplugins/plugins.qmlproject @@ -0,0 +1,18 @@ +import QmlProject 1.1 + +Project { + mainFile: "plugins.qml" + + /* Include .qml, .js, and image files from current directory and subdirectories */ + QmlFiles { + directory: "." + } + JavaScriptFiles { + directory: "." + } + ImageFiles { + directory: "." + } + + importPaths: [ "imports" ] +} diff --git a/tests/manual/qmlextensionplugins/qmlextensionplugins.pro b/tests/manual/qmlextensionplugins/qmlextensionplugins.pro new file mode 100644 index 0000000000..c074b8d671 --- /dev/null +++ b/tests/manual/qmlextensionplugins/qmlextensionplugins.pro @@ -0,0 +1,46 @@ +TEMPLATE = lib +CONFIG += plugin qmltypes +QT += qml + +QML_IMPORT_NAME = TimeExample +QML_IMPORT_MAJOR_VERSION = 1 + +DESTDIR = imports/$$QML_IMPORT_NAME +TARGET = qmlqtimeexampleplugin +QMLTYPES_FILENAME = $$DESTDIR/plugins.qmltypes + +SOURCES += \ + plugin.cpp \ + timemodel.cpp + +HEADERS += \ + timemodel.h + +PLUGINFILES = \ + imports/$$QML_IMPORT_NAME/qmldir \ + imports/$$QML_IMPORT_NAME/center.png \ + imports/$$QML_IMPORT_NAME/clock.png \ + imports/$$QML_IMPORT_NAME/Clock.qml \ + imports/$$QML_IMPORT_NAME/hour.png \ + imports/$$QML_IMPORT_NAME/minute.png + +target.path = $$[QT_INSTALL_EXAMPLES]/qml/qmlextensionplugins/imports/$$QML_IMPORT_NAME + +pluginfiles_copy.files = $$PLUGINFILES +pluginfiles_copy.path = $$DESTDIR + +pluginfiles_install.files = $$PLUGINFILES $$OUT_PWD/$$DESTDIR/plugins.qmltypes +pluginfiles_install.path = $$[QT_INSTALL_EXAMPLES]/qml/qmlextensionplugins/imports/$$QML_IMPORT_NAME + +qml_copy.files = plugins.qml plugins.qmlproject +qml_copy.path = $$OUT_PWD + +qml_install.files = plugins.qml plugins.qmlproject +qml_install.path = $$[QT_INSTALL_EXAMPLES]/qml/qmlextensionplugins + +INSTALLS += target qml_install pluginfiles_install +COPIES += qml_copy pluginfiles_copy + +OTHER_FILES += $$PLUGINFILES plugins.qml + +CONFIG += install_ok # Do not cargo-cult this! diff --git a/tests/manual/qmlextensionplugins/timemodel.cpp b/tests/manual/qmlextensionplugins/timemodel.cpp new file mode 100644 index 0000000000..1e539b430d --- /dev/null +++ b/tests/manual/qmlextensionplugins/timemodel.cpp @@ -0,0 +1,51 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "timemodel.h" + +int TimeModel::instances=0; +MinuteTimer *TimeModel::timer=nullptr; + +void MinuteTimer::start() +{ + if (!timer.isActive()) { + time = QTime::currentTime(); + timer.start(60000-time.second()*1000, this); + } +} + +void MinuteTimer::stop() +{ + timer.stop(); +} + +void MinuteTimer::timerEvent(QTimerEvent *) +{ + QTime now = QTime::currentTime(); + if (now.second() == 59 && now.minute() == time.minute() && now.hour() == time.hour()) { + // just missed time tick over, force it, wait extra 0.5 seconds + time = time.addSecs(60); + timer.start(60500, this); + } else { + time = now; + timer.start(60000-time.second()*1000, this); + } + emit timeChanged(); +} + +TimeModel::TimeModel(QObject *parent) : QObject(parent) +{ + if (++instances == 1) { + if (!timer) + timer = new MinuteTimer(QCoreApplication::instance()); + connect(timer, &MinuteTimer::timeChanged, this, &TimeModel::timeChanged); + timer->start(); + } +} + +TimeModel::~TimeModel() +{ + if (--instances == 0) { + timer->stop(); + } +} diff --git a/tests/manual/qmlextensionplugins/timemodel.h b/tests/manual/qmlextensionplugins/timemodel.h new file mode 100644 index 0000000000..5ecd6d2d61 --- /dev/null +++ b/tests/manual/qmlextensionplugins/timemodel.h @@ -0,0 +1,64 @@ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef TIMEMODEL_H +#define TIMEMODEL_H + +#include <QtQml/qqml.h> +#include <QtCore/qdatetime.h> +#include <QtCore/qbasictimer.h> +#include <QtCore/qcoreapplication.h> + +// Implements a "TimeModel" class with hour and minute properties +// that change on-the-minute yet efficiently sleep the rest +// of the time. + +class MinuteTimer : public QObject +{ + Q_OBJECT +public: + MinuteTimer(QObject *parent) : QObject(parent) {} + + void start(); + void stop(); + + int hour() const { return time.hour(); } + int minute() const { return time.minute(); } + +signals: + void timeChanged(); + +protected: + void timerEvent(QTimerEvent *) override; + +private: + QTime time; + QBasicTimer timer; +}; + +//![0] +class TimeModel : public QObject +{ + Q_OBJECT + Q_PROPERTY(int hour READ hour NOTIFY timeChanged) + Q_PROPERTY(int minute READ minute NOTIFY timeChanged) + QML_NAMED_ELEMENT(Time) +//![0] + +public: + TimeModel(QObject *parent=nullptr); + ~TimeModel() override; + + int minute() const { return timer->minute(); } + int hour() const { return timer->hour(); } + +signals: + void timeChanged(); + +private: + QTime t; + static MinuteTimer *timer; + static int instances; +}; + +#endif // TIMEMODEL_H |