PonyPlayer
hotloader.hpp
浏览该文件的文档.
1//
2// Created by ColorsWind on 2022/5/2.
3//
4
5#ifndef PONYPLAYER_HOTLOADER_H
6#define PONYPLAYER_HOTLOADER_H
7#include <QtCore>
8#include <QQmlApplicationEngine>
9#include <QQuickView>
10#include <filesystem>
11
17class HotLoader : public QObject {
18 Q_OBJECT
19private:
20 QQmlApplicationEngine *engine;
21public:
22 explicit HotLoader(QQmlApplicationEngine *e): engine(e) {
23 qDebug() << "Construct HotLoader.";
24 }
25
26 Q_INVOKABLE void reload() {
27#ifdef QT_DEBUG
28 static std::string mainQML = std::filesystem::path(__FILE__).parent_path().string() + "/view/main.qml";
29 auto *rootObject = engine->rootObjects().first();
30 auto* mainWindow = qobject_cast<QQuickWindow*>(rootObject);
31 Q_ASSERT( mainWindow != nullptr );
32 mainWindow->close();
33 engine->clearComponentCache();
34 mainWindow->deleteLater();
35 engine->load(QUrl::fromLocalFile(QString::fromUtf8(mainQML)));
36 qWarning() << "Complete hot reloading.";
37#else
38 Q_UNUSED(engine)
39#endif
40 }
41
42 Q_INVOKABLE void crash() {
43#ifdef QT_DEBUG
44 throw std::runtime_error("Crash Test!");
45#endif
46 }
47};
48
49#endif //PONYPLAYER_HOTLOADER_H
提供热重载支持
Definition: hotloader.hpp:17
HotLoader(QQmlApplicationEngine *e)
Definition: hotloader.hpp:22
Q_INVOKABLE void reload()
Definition: hotloader.hpp:26
Q_INVOKABLE void crash()
Definition: hotloader.hpp:42