-
Notifications
You must be signed in to change notification settings - Fork 29
/
main-QQmlApplicationEngine.cpp
69 lines (56 loc) · 1.58 KB
/
main-QQmlApplicationEngine.cpp
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
#include <QtGui/QGuiApplication>
#include <QFileSystemWatcher>
#include <QtQuick/QQuickView>
#include <QDebug>
#include <QQmlEngine>
#include <QThread>
#include <QFileInfo>
#include <QQmlApplicationEngine>
class LiveReload: public QGuiApplication {
Q_OBJECT
public:
LiveReload(int argc, char* argv[]);
QFileSystemWatcher watcher;
// QQuickView view;
QQmlApplicationEngine engine;
private:
QString filename;
void show(){
qobject_cast<QQuickWindow *>(engine.rootObjects().value(0))->show();
}
private slots:
void fileChanged(const QString & path) {
qDebug() << "file changed: " << path;
// view.engine()->clearComponentCache();
// QThread::msleep(50);
// view.setSource(QUrl(filename));
// view.show();
engine.clearComponentCache();
QThread::msleep(50);
engine.load(QUrl::fromLocalFile(filename));
show();
};
};
LiveReload::LiveReload(int argc, char* argv[]):
QGuiApplication(argc,argv) {
filename = "./main.qml";
if (argc>1) filename = argv[1];
QFileInfo file(filename);
if (!file.exists()) qFatal("File not found.");
setApplicationName("Livereload:"+file.baseName());
watcher.addPath(file.absolutePath());
watcher.addPath(filename);
connect(&watcher, SIGNAL(directoryChanged(const QString &)),
this, SLOT(fileChanged(const QString &)));
connect(&watcher, SIGNAL(fileChanged(const QString &)),
this, SLOT(fileChanged(const QString &)));
// view.setSource(QUrl(filename));
// view.show();
engine.load(QUrl::fromLocalFile(filename));
show();
}
int main(int argc, char* argv[]) {
LiveReload app(argc,argv);
return app.exec();
}
#include "main.moc"