Skip to content

Commit

Permalink
Correct qml6-singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
herzenschein committed Jun 9, 2024
1 parent a30d65c commit 82fbb5a
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 44 deletions.
50 changes: 28 additions & 22 deletions qml6-singleton/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,46 @@ 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,
# so avoid using qt_add_resources() with it.
# 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)
31 changes: 16 additions & 15 deletions qml6-singleton/main.qml → qml6-singleton/Main.qml
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion qml6-singleton/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 20 additions & 0 deletions qml6-singleton/qml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -43,5 +42,10 @@ Dialog {

onTextChanged: MySingleton.thing = text
}
Button {
Layout.alignment: Qt.AlignCenter
text: "Confirm"
onClicked: root.close()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 82fbb5a

Please sign in to comment.