-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogichandler.cpp
More file actions
72 lines (57 loc) · 2.73 KB
/
logichandler.cpp
File metadata and controls
72 lines (57 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "logichandler.h" // This is the header file that defines the LogicHandler class
#include <QWindow> // This provides access to window-related functionality in Qt
#include <QDebug> // This is used for logging debug messages to the console
#include <QCoreApplication> // This provides the application framework and allows for
// application-wide actions such as exiting the application
LogicHandler::LogicHandler(QObject *parent)
: QObject(parent), m_newEngine(nullptr) {}
// QObject(parent) initializes the parent object and ensures proper memory management
// which means it will delete this object when appropriate
// m_newEngine(nullptr) initializes the pointer m_newEngine to nullptr, indicating that
// no new QQmlApplicationEngine instance has been created yet
// This function is triggered when the user wants to navigate to a specific QML page
void LogicHandler::handleNavigation(const QString &qmlFile, QObject *currentWindow)
{
qDebug() << "Navigating to:" << qmlFile;
// This outputs a debug message indicating the target QML file being loaded
// This ensures only one QQmlApplicationEngine instance is created
// If multiple instances of QQmlApplicationEngine are created, it can increase
// memory usage and performance overhead
if (!m_newEngine) {
// If m_newEngine is nullptr, a new engine is created to manage and render the new QML interface
m_newEngine = new QQmlApplicationEngine(); // Persistent engine for the new window
}
// Load the specified QML file from the Qt Resource System
m_newEngine->load(QUrl(QStringLiteral("qrc:/") + qmlFile));
// Error handling
// If the loaded QML file has no root objects, it indicates a loading failure
if (m_newEngine->rootObjects().isEmpty()) {
qWarning() << "Failed to load" << qmlFile;
return;
}
// Close the current window
// This attempts to cast currentWindow to a QWindow pointer.
// If successful, it represents the current application's window
QWindow *window = qobject_cast<QWindow *>(currentWindow);
if (window) {
window->close();
}
}
void LogicHandler::handleNavigation(const QString &qmlFile) {
qDebug() << "Navigating to:" << qmlFile;
if (!m_newEngine) {
m_newEngine = new QQmlApplicationEngine();
}
m_newEngine->load(QUrl(QStringLiteral("qrc:/") + qmlFile));
if (m_newEngine->rootObjects().isEmpty()) {
qWarning() << "Failed to load" << qmlFile;
return;
}
}
// This function is triggered when the user clicks the "No" button.
void LogicHandler::handleNoClicked()
{
qDebug() << "User chose NO - Exiting application.";
QCoreApplication::quit(); // Exit the application
// This ends the event loop and releases resources
}