status-qml/sandbox/sandboxapp.cpp

56 lines
1.5 KiB
C++
Raw Normal View History

#include "sandboxapp.h"
#include <QQmlContext>
#include <QWindow>
#include <QDebug>
2021-05-20 06:25:52 +00:00
#include "statuswindow.h"
#include "spellchecker.h"
2021-05-20 06:25:52 +00:00
SandboxApp::SandboxApp(int &argc, char **argv)
: QGuiApplication(argc, argv),
m_handler(new Handler(this))
{
connect(m_handler, &Handler::restartQml, this, &SandboxApp::restartEngine, Qt::QueuedConnection);
}
void SandboxApp::startEngine()
{
2021-05-20 06:25:52 +00:00
qmlRegisterType<StatusWindow>("Sandbox", 0, 1, "StatusWindow");
qmlRegisterType<SpellChecker>("Sandbox", 0, 1, "Spellchecker");
2021-05-20 06:25:52 +00:00
#ifdef QT_DEBUG
2022-01-11 14:49:24 +00:00
const QUrl url = QUrl::fromLocalFile(applicationDirPath() + "/../main.qml");
#else
const QUrl url(QStringLiteral("qrc:/main.qml"));
#endif
m_engine.rootContext()->setContextProperty("app", m_handler);
#ifdef QT_DEBUG
m_engine.addImportPath(applicationDirPath() + "/../../src");
#else
m_engine.addImportPath(QStringLiteral("qrc:/src"));
#endif
qDebug() << m_engine.importPathList();
QObject::connect(&m_engine, &QQmlApplicationEngine::objectCreated,
this, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
m_engine.load(url);
}
void SandboxApp::restartEngine()
{
const QUrl url(applicationDirPath() + "/../main.qml");
QWindow *rootWindow = qobject_cast<QWindow*>(m_engine.rootObjects().at(0));
if (rootWindow) {
rootWindow->close();
rootWindow->deleteLater();
}
m_engine.clearComponentCache();
m_engine.load(url);
}