diff --git a/qml6-singleton/CMakeLists.txt b/qml6-singleton/CMakeLists.txt index 1521bf4..305ebad 100644 --- a/qml6-singleton/CMakeLists.txt +++ b/qml6-singleton/CMakeLists.txt @@ -10,10 +10,14 @@ find_package(Qt6 REQUIRED COMPONENTS Quick) # Autoconfigures AUTOMOC, AUTORCC, AUTOUIC etc. qt_standard_project_setup() +qt_policy(SET QTP0001 NEW) + # Create a target executable -qt_add_executable(${PROJECT_NAME} - main.cpp - singleton.cpp singleton.hpp +qt_add_executable(${PROJECT_NAME}) + +target_sources(${PROJECT_NAME} + PRIVATE + "main.cpp" ) # This automatically creates resources, @@ -21,29 +25,31 @@ qt_add_executable(${PROJECT_NAME} # If we had a qrc file, we'd put after the first argument. # This generates a resource like this: # qrc: + RESOURCE_PREFIX + URI + / + QML_FILES -# so qrc:/SingletonExample/main.qml +# so qrc:/qt/qml/com/example/singleton/Main.qml qt_add_qml_module(${PROJECT_NAME} - URI "SingletonExample" + URI "com.example.singleton" VERSION 1.0 - RESOURCE_PREFIX "/" QML_FILES - "main.qml" - "SetDialog.qml" - "ViewDialog.qml" - SOURCES - "main.cpp" + "Main.qml" ) -qt_add_qml_module(singleton # This automatically creates a singletonplugin target. - URI "SingletonImport" - VERSION 1.0 - RESOURCE_PREFIX "/" - OUTPUT_DIRECTORY "SingletonImport" - SOURCES - "singleton.cpp" - "singleton.hpp" - STATIC +# Finish by linking the necessary Qt libraries, including our custom target/module +target_link_libraries(${PROJECT_NAME} + PRIVATE + Qt6::Quick + singleton-singletons ) -# Finish by linking the necessary Qt libraries, including our custom target/module -target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Quick singletonplugin) +install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_qml_app_script( + TARGET ${PROJECT_NAME} + OUTPUT_SCRIPT deployScript +) + +install(SCRIPT ${deployScript}) + +add_subdirectory(qml) diff --git a/qml6-singleton/main.qml b/qml6-singleton/Main.qml similarity index 72% rename from qml6-singleton/main.qml rename to qml6-singleton/Main.qml index ef468a4..e593de9 100644 --- a/qml6-singleton/main.qml +++ b/qml6-singleton/Main.qml @@ -1,40 +1,41 @@ import QtQuick +import QtQuick.Layouts import QtQuick.Controls import QtQuick.Controls.Material -import QtQuick.Layouts + +import com.example.singleton.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 - text: "Two different dialogs in two different modules accessing the same data via MySingleton." + 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/qml6-singleton/main.cpp b/qml6-singleton/main.cpp index 1df361d..9394445 100644 --- a/qml6-singleton/main.cpp +++ b/qml6-singleton/main.cpp @@ -6,7 +6,7 @@ int main(int argCount, char* argVector[]) QGuiApplication singletonApp(argCount, argVector); QQmlApplicationEngine engine; - engine.load("qrc:/SingletonExample/main.qml"); + engine.loadFromModule("com.example.singleton", "Main"); singletonApp.exec(); } diff --git a/qml6-singleton/qml/CMakeLists.txt b/qml6-singleton/qml/CMakeLists.txt new file mode 100644 index 0000000..b6fd502 --- /dev/null +++ b/qml6-singleton/qml/CMakeLists.txt @@ -0,0 +1,20 @@ +qt_add_library(singleton-dialogs) +qt_add_library(singleton-singletons) + +qt_add_qml_module(pragmasingleton-dialogs + URI "com.example.singleton.dialogs" + VERSION 1.0 + OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/com/example/singleton/dialogs" + QML_FILES + "SetDialog.qml" + "ViewDialog.qml" +) + +qt_add_qml_module(singleton-singletons + URI "com.example.singleton.singletons" + VERSION 1.0 + OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/com/example/singleton/singletons" + SOURCES + "singleton.cpp" + "singleton.hpp" +) diff --git a/qml6-singleton/SetDialog.qml b/qml6-singleton/qml/SetDialog.qml similarity index 82% rename from qml6-singleton/SetDialog.qml rename to qml6-singleton/qml/SetDialog.qml index 83957e1..f1a2d4b 100644 --- a/qml6-singleton/SetDialog.qml +++ b/qml6-singleton/qml/SetDialog.qml @@ -1,13 +1,12 @@ import QtQuick +import QtQuick.Layouts import QtQuick.Controls import QtQuick.Controls.Material -import QtQuick.Layouts -// This comes from the singleton module we created -import SingletonImport +import com.example.singleton.singletons Dialog { - + id: root ColumnLayout { anchors.fill: parent @@ -43,5 +42,10 @@ Dialog { onTextChanged: MySingleton.thing = text } + Button { + Layout.alignment: Qt.AlignCenter + text: "Confirm" + onClicked: root.close() + } } } diff --git a/qml6-singleton/ViewDialog.qml b/qml6-singleton/qml/ViewDialog.qml similarity index 92% rename from qml6-singleton/ViewDialog.qml rename to qml6-singleton/qml/ViewDialog.qml index 9c2c32f..48c13fd 100644 --- a/qml6-singleton/ViewDialog.qml +++ b/qml6-singleton/qml/ViewDialog.qml @@ -3,8 +3,7 @@ import QtQuick.Controls import QtQuick.Controls.Material import QtQuick.Layouts -// This comes from the singleton module we created -import SingletonImport +import com.example.singleton.singletons Dialog { diff --git a/qml6-singleton/singleton.cpp b/qml6-singleton/qml/singleton.cpp similarity index 100% rename from qml6-singleton/singleton.cpp rename to qml6-singleton/qml/singleton.cpp diff --git a/qml6-singleton/singleton.hpp b/qml6-singleton/qml/singleton.hpp similarity index 100% rename from qml6-singleton/singleton.hpp rename to qml6-singleton/qml/singleton.hpp