Last active
May 27, 2024 16:50
-
-
Save nrbnlulu/195bdf87138bfc57b99a56ee63a53277 to your computer and use it in GitHub Desktop.
QML live-reload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QFileSystemWatcher> | |
#include <QQmlApplicationEngine> | |
#include <QTimer> | |
#include <QUrl> | |
#include <QDirIterator> | |
void init_live_reload(QQmlApplicationEngine *engine, const QString &path) { | |
auto *fs_watcher = new QFileSystemWatcher(engine); | |
QDirIterator it(path, QDirIterator::Subdirectories); | |
while (it.hasNext()) { | |
it.next(); | |
auto i = it.fileInfo(); | |
if (i.isFile()) | |
fs_watcher->addPath(i.absoluteFilePath()); | |
} | |
QObject::connect(fs_watcher, &QFileSystemWatcher::fileChanged, [=](const QString &file) { | |
QTimer::singleShot(50, [=] { | |
static QQuickItem *previousItem = nullptr; | |
auto window = qobject_cast<QQuickWindow *>(engine->rootObjects().first()); | |
fs_watcher->addPath(file); | |
auto loader = window->findChild<QObject*>("AppLoader"); | |
auto prev_source = loader->property("source"); | |
loader->setProperty("source", ""); | |
engine->clearComponentCache(); | |
loader->setProperty("source", prev_source); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui> | |
#include <QtQuick> | |
#include <filesystem> | |
#include <QQmlComponent> | |
#include "hotreload.hpp" | |
namespace fs = std::filesystem; | |
int main(int argc, char *argv[]) { | |
QGuiApplication app(argc, argv); | |
QQmlApplicationEngine engine; | |
init_live_reload(&engine, "path/to/root/qml"); | |
QUrl url((fs::path(__FILE__).parent_path() / "qml" / "mainwindow.qml").c_str()); | |
engine.load(url); | |
return QGuiApplication::exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// every time this file is edited the loader will refresh it. | |
import QtQuick | |
import QtQuick.Controls | |
import QtQuick.Controls.Material | |
import DogModule | |
Pane { | |
anchors.fill: parent; | |
Rectangle{ | |
width:234; | |
height: 234; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick | |
import QtQuick.Controls | |
import QtQuick.Controls.Material | |
ApplicationWindow { | |
id: root | |
width: 500 | |
height: 400 | |
visible: true | |
Material.theme: Material.Dark | |
Material.accent: Material.Orange | |
Loader{objectName: "AppLoader" | |
source: "./Main.qml" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah QML tooling's are awful...