diff --git a/qml5-singletoninstance/CMakeLists.txt b/qml5-singletoninstance/CMakeLists.txt index b42129d..3e805be 100644 --- a/qml5-singletoninstance/CMakeLists.txt +++ b/qml5-singletoninstance/CMakeLists.txt @@ -6,28 +6,34 @@ find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup() +qt_policy(SET QTP0001 NEW) + qt_add_executable(${PROJECT_NAME} main.cpp - singletoninstance.cpp - singletoninstance.hpp ) -# The qmlRegisterSingletonInstance method -# does not take advantage of qt_add_qml_module. -# If we didn't use this Qt6 feature here, -# we'd have to use qrc files and would end up with: -# SingletonImport in C++ -# main.qml in qrc -# qrc in CMakeLists.txt -# What a mess. qt_add_qml_module(${PROJECT_NAME} - URI "Main" + URI "com.example.singletoninstance" VERSION 1.0 - RESOURCE_PREFIX "/" QML_FILES - "main.qml" - "SetDialog.qml" - "ViewDialog.qml" + "Main.qml" + SOURCES + "singletoninstance.cpp" + "singletoninstance.hpp" +) + +target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}-dialogs Qt6::Quick) + +install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ) -target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Quick) +qt_generate_deploy_qml_app_script( + TARGET ${PROJECT_NAME} + OUTPUT_SCRIPT deployScript +) + +install(SCRIPT ${deployScript}) + +add_subdirectory(qml) diff --git a/qml5-singletoninstance/main.qml b/qml5-singletoninstance/Main.qml similarity index 75% rename from qml5-singletoninstance/main.qml rename to qml5-singletoninstance/Main.qml index 3fff0a9..00c81e1 100644 --- a/qml5-singletoninstance/main.qml +++ b/qml5-singletoninstance/Main.qml @@ -1,40 +1,41 @@ import QtQuick +import QtQuick.Layouts import QtQuick.Controls import QtQuick.Controls.Material -import QtQuick.Layouts + +import com.example.singletoninstance.dialogs ApplicationWindow { title: "Minimal QML6 Singleton Example" visible: true - minimumWidth: 600 + minimumWidth: 700 + width: 700 + height: 700 Material.theme: Material.Dark ColumnLayout { - anchors.fill: parent - - Item { - Layout.fillHeight: true - } + anchors.left: parent.left + anchors.right: parent.right Label { - Layout.fillWidth: true - Layout.fillHeight: false - horizontalAlignment: Qt.AlignHCenter + Layout.alignment: Qt.AlignCenter + Layout.topMargin: height text: "Two different dialogs in two different modules accessing the same data via SingletonInstance." } + Item { + Layout.fillHeight: true + } Button { - Layout.fillWidth: true - Layout.fillHeight: false + Layout.alignment: Qt.AlignCenter text: "Click me to set the Singleton properties" onClicked: setDialog.open() } Button { - Layout.fillWidth: true - Layout.fillHeight: false + Layout.alignment: Qt.AlignCenter text: "Click me to view the Singleton properties" onClicked: viewDialog.open() diff --git a/qml5-singletoninstance/main.cpp b/qml5-singletoninstance/main.cpp index 0c3f89b..48c5824 100644 --- a/qml5-singletoninstance/main.cpp +++ b/qml5-singletoninstance/main.cpp @@ -12,7 +12,8 @@ int main(int argCount, char* argVector[]) SingletonInstance::get()); // or simply get() if out of class. QQmlApplicationEngine engine; - engine.load("qrc:/Main/main.qml"); + // engine.load("qrc:/qt/qml/com/example/singletoninstance/Main.qml"); + engine.loadFromModule("com.example.singletoninstance", "Main"); singletonInstanceApp.exec(); } diff --git a/qml5-singletoninstance/qml/CMakeLists.txt b/qml5-singletoninstance/qml/CMakeLists.txt new file mode 100644 index 0000000..61bc003 --- /dev/null +++ b/qml5-singletoninstance/qml/CMakeLists.txt @@ -0,0 +1,11 @@ +qt_add_library(${PROJECT_NAME}-dialogs) + +qt_add_qml_module(${PROJECT_NAME}-dialogs + URI "com.example.singletoninstance.dialogs" + VERSION 1.0 + OUTPUT_DIRECTORY + "${CMAKE_BINARY_DIR}/com/example/singletoninstance/dialogs" + QML_FILES + "SetDialog.qml" + "ViewDialog.qml" +) diff --git a/qml5-singletoninstance/SetDialog.qml b/qml5-singletoninstance/qml/SetDialog.qml similarity index 86% rename from qml5-singletoninstance/SetDialog.qml rename to qml5-singletoninstance/qml/SetDialog.qml index 2e4e8b4..df79123 100644 --- a/qml5-singletoninstance/SetDialog.qml +++ b/qml5-singletoninstance/qml/SetDialog.qml @@ -1,13 +1,13 @@ import QtQuick +import QtQuick.Layouts import QtQuick.Controls import QtQuick.Controls.Material -import QtQuick.Layouts // This comes from qmlRegisterSingletonInstance() import SingletonImport Dialog { - + id: root ColumnLayout { anchors.fill: parent @@ -43,5 +43,10 @@ Dialog { onTextChanged: SingletonInstance.thing = text } + Button { + Layout.alignment: Qt.AlignCenter + text: "Confirm" + onClicked: root.close() + } } } diff --git a/qml5-singletoninstance/ViewDialog.qml b/qml5-singletoninstance/qml/ViewDialog.qml similarity index 100% rename from qml5-singletoninstance/ViewDialog.qml rename to qml5-singletoninstance/qml/ViewDialog.qml