Skip to content

Commit e6b0fd2

Browse files
committed
Simplify examples and add more comments
1 parent 2056dce commit e6b0fd2

File tree

10 files changed

+49
-43
lines changed

10 files changed

+49
-43
lines changed

examples/AdvancedPy/src/AdvancedPy/Gui/Globals/BackendWrapper.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ QtObject {
2121

2222
readonly property var activeBackend: {
2323
if (typeof Backends.PyBackend !== 'undefined') {
24-
console.debug('Currently, the REAL python backend is in use')
24+
console.debug('REAL python backend is in use')
2525
return Backends.PyBackend
2626
} else {
27-
console.debug('Currently, the MOCK backend is in use')
27+
console.debug('MOCK QML backend is in use')
2828
return Backends.MockBackend
2929
}
3030
}

examples/AdvancedPy/src/AdvancedPy/main.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
qInstallMessageHandler(console.qmlMessageHandler)
2626
console.debug('Custom Qt message handler defined')
2727

28+
# This singleton object will be accessible in QML as follows:
29+
# import Backends 1.0 as Backends OR import Backends as Backends
30+
# property var activeBackend: Backends.PyBackend
31+
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
32+
console.debug('Backend class is registered as a singleton type for QML')
33+
2834
app = QGuiApplication(sys.argv)
2935
console.debug(f'Qt Application created {app}')
3036

3137
engine = QQmlApplicationEngine()
3238
console.debug(f'QML application engine created {engine}')
3339

34-
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
35-
console.debug('Backend class is registered to be accessible from QML via the name PyBackend')
36-
37-
engine.addImportPath(EASYAPP_DIR)
3840
engine.addImportPath(CURRENT_DIR)
41+
engine.addImportPath(EASYAPP_DIR)
3942
console.debug('Paths added where QML searches for components')
4043

4144
engine.load(CURRENT_DIR / 'main.qml')

examples/BasicC++/src/BasicC++/Gui/Globals/BackendWrapper.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ QtObject {
2121

2222
readonly property var activeBackend: {
2323
if (typeof Backends.PyBackend !== 'undefined') {
24-
console.debug('Currently, the REAL python backend is in use')
24+
console.debug('REAL python backend is in use')
2525
return Backends.PyBackend
2626
} else {
27-
console.debug('Currently, the MOCK backend is in use')
27+
console.debug('MOCK QML backend is in use')
2828
return Backends.MockBackend
2929
}
3030
}

examples/BasicC++/src/BasicC++/main.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
#include <QQmlApplicationEngine>
77
#include <QString>
88

9-
const QString CURRENT_DIR = "qrc:/"; // path to qml components of the easyapp module
10-
const QString EASYAPP_DIR = "qrc:/../../../../src/EasyApp"; // path to qml components of the current project
119

1210
int main(int argc, char *argv[])
1311
{
14-
// Create application
12+
// Create Qt application
1513
QGuiApplication app(argc, argv);
1614

17-
// Create QML application engine
15+
// Create the QML application engine
1816
QQmlApplicationEngine engine;
19-
engine.addImportPath(CURRENT_DIR);
20-
engine.addImportPath(EASYAPP_DIR);
17+
18+
// Add the paths where QML searches for components
19+
// Use whatever is defined in the resources.qrc file
20+
engine.addImportPath("qrc:/");
21+
22+
// Load the main QML component
2123
engine.load("qrc:/main.qml");
2224

23-
// Event loop
25+
// Start the application event loop
2426
if (engine.rootObjects().isEmpty())
2527
return -1;
2628
return app.exec();

examples/BasicPy/src/BasicPy/Gui/Globals/BackendWrapper.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ QtObject {
2121

2222
readonly property var activeBackend: {
2323
if (typeof Backends.PyBackend !== 'undefined') {
24-
console.debug('Currently, the REAL python backend is in use')
24+
console.debug('REAL python backend is in use')
2525
return Backends.PyBackend
2626
} else {
27-
console.debug('Currently, the MOCK backend is in use')
27+
console.debug('MOCK QML backend is in use')
2828
return Backends.MockBackend
2929
}
3030
}

examples/BasicPy/src/BasicPy/main.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,20 @@
1717

1818

1919
if __name__ == '__main__':
20-
# Create application
20+
# Create Qt application
2121
app = QGuiApplication(sys.argv)
2222

23-
# Create QML application engine
23+
# Create the QML application engine
2424
engine = QQmlApplicationEngine()
25-
engine.addImportPath(EASYAPP_DIR)
25+
26+
# Add the paths where QML searches for components
2627
engine.addImportPath(CURRENT_DIR)
28+
engine.addImportPath(EASYAPP_DIR)
29+
30+
# Load the main QML component
2731
engine.load(CURRENT_DIR / 'main.qml')
2832

29-
# Event loop
33+
# Start the application event loop
3034
if not engine.rootObjects():
3135
sys.exit(-1)
3236
sys.exit(app.exec())

examples/BasicQml/src/BasicQml/Gui/Globals/BackendWrapper.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ QtObject {
2121

2222
readonly property var activeBackend: {
2323
if (typeof Backends.PyBackend !== 'undefined') {
24-
console.debug('Currently, the REAL python backend is in use')
24+
console.debug('REAL python backend is in use')
2525
return Backends.PyBackend
2626
} else {
27-
console.debug('Currently, the MOCK backend is in use')
27+
console.debug('MOCK QML backend is in use')
2828
return Backends.MockBackend
2929
}
3030
}

examples/IntermediatePy/src/IntermediatePy/Backends/real_py/project.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def name(self):
9494
def name(self, new_value):
9595
if self._name == new_value:
9696
return
97-
console.debug(IO.format_msg('main', f"Changing project name from '{self.name}' to '{new_value}'"))
97+
print(f" py: Changing project name from '{self.name}' to '{new_value}'")
9898
self._name = new_value
9999
self.nameChanged.emit()
100100

@@ -113,19 +113,19 @@ def examples(self):
113113

114114
@Slot()
115115
def create(self):
116-
console.debug(IO.format_msg('main', f"Creating project '{self.name}'"))
116+
print(f" py: Creating project '{self.name}'")
117117
self.info['creationDate'] = time.strftime("%d %b %Y %H:%M", time.localtime())
118118
self.infoChanged.emit()
119119
self.created = True
120120

121121
@Slot()
122122
def save(self):
123-
console.debug(IO.format_msg('main', f"Saving project '{self.name}'"))
123+
print(f" py: Saving project '{self.name}'")
124124

125125
@Slot(str, str)
126126
def editInfo(self, path, new_value):
127127
if DottyDict.get(self._info, path) == new_value:
128128
return
129-
console.debug(IO.format_msg('main', f"Changing project info.{path} from '{DottyDict.get(self._info, path)}' to '{new_value}'"))
129+
print(f" py: Changing project info.{path} from '{DottyDict.get(self._info, path)}' to '{new_value}'")
130130
DottyDict.set(self._info, path, new_value)
131131
self.infoChanged.emit()

examples/IntermediatePy/src/IntermediatePy/Gui/Globals/BackendWrapper.qml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ QtObject {
2121

2222
readonly property var activeBackend: {
2323
if (typeof Backends.PyBackend !== 'undefined') {
24-
console.debug('Currently, the REAL python backend is in use')
24+
console.debug('REAL python backend is in use')
2525
return Backends.PyBackend
2626
} else {
27-
console.debug('Currently, the MOCK backend is in use')
27+
console.debug('MOCK QML backend is in use')
2828
return Backends.MockBackend
2929
}
3030
}

examples/IntermediatePy/src/IntermediatePy/main.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from PySide6.QtGui import QGuiApplication
99
from PySide6.QtQml import QQmlApplicationEngine, qmlRegisterSingletonType
10-
from PySide6.QtCore import qInstallMessageHandler
1110

1211
# It is usually assumed that the EasyApp package is already installed in the desired python environment.
1312
# If this is not the case, and if the example is run from the EasyApp repository, one need to add the path to the
@@ -16,32 +15,30 @@
1615
EASYAPP_DIR = CURRENT_DIR / '..' / '..' / '..' / '..' / 'src' # path to qml components of the easyapp module
1716
sys.path.append(str(EASYAPP_DIR))
1817

19-
from EasyApp.Logic.Logging import console
20-
2118
from Backends.real_backend import Backend
2219

2320

2421
if __name__ == '__main__':
25-
qInstallMessageHandler(console.qmlMessageHandler)
26-
console.debug('Custom Qt message handler defined')
22+
# Register the Backend class as a singleton type for QML
23+
# This singleton object will be accessible in QML as follows:
24+
# import Backends 1.0 as Backends OR import Backends as Backends
25+
# property var activeBackend: Backends.PyBackend
26+
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
2727

28+
# Create Qt application
2829
app = QGuiApplication(sys.argv)
29-
console.debug(f'Qt Application created {app}')
3030

31+
# Create the QML application engine
3132
engine = QQmlApplicationEngine()
32-
console.debug(f'QML application engine created {engine}')
33-
34-
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
35-
console.debug('Backend class is registered to be accessible from QML via the name PyBackend')
3633

37-
engine.addImportPath(EASYAPP_DIR)
34+
# Add the paths where QML searches for components
3835
engine.addImportPath(CURRENT_DIR)
39-
console.debug('Paths added where QML searches for components')
36+
engine.addImportPath(EASYAPP_DIR)
4037

38+
# Load the main QML component
4139
engine.load(CURRENT_DIR / 'main.qml')
42-
console.debug('Main QML component loaded')
4340

44-
console.debug('Application event loop is about to start')
41+
# Start the application event loop
4542
if not engine.rootObjects():
4643
sys.exit(-1)
4744
sys.exit(app.exec())

0 commit comments

Comments
 (0)