Skip to content

Commit

Permalink
Correct qml5-singletoninstance
Browse files Browse the repository at this point in the history
  • Loading branch information
herzenschein committed Jun 9, 2024
1 parent d8d31fc commit 49ddf87
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 33 deletions.
38 changes: 22 additions & 16 deletions qml5-singletoninstance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 15 additions & 14 deletions qml5-singletoninstance/main.qml → qml5-singletoninstance/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.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()
Expand Down
3 changes: 2 additions & 1 deletion qml5-singletoninstance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
11 changes: 11 additions & 0 deletions qml5-singletoninstance/qml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
@@ -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

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

onTextChanged: SingletonInstance.thing = text
}
Button {
Layout.alignment: Qt.AlignCenter
text: "Confirm"
onClicked: root.close()
}
}
}
File renamed without changes.

0 comments on commit 49ddf87

Please sign in to comment.