Вот что я сделал и работает нормально:
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "app.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
QQmlApplicationEngine engine;
App appp;
appp.setEngine(&engine);
engine.rootContext()->setContextProperty("app", &appp);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
app.cpp:
#include "app.h"
App::App(QObject *parent): QObject (parent)
{
}
App::~App(){}
void App::loadQml()
{
m_engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
}
app.h:
#ifndef APP_H
#define APP_H
#include <QObject>
#include <QQmlApplicationEngine>
class App: public QObject
{
Q_OBJECT
public:
App(QObject* parent = Q_NULLPTR);
~App();
Q_INVOKABLE void loadQml();
void setEngine(QQmlApplicationEngine *engine) { m_engine = engine; }
private:
QQmlApplicationEngine* m_engine;
};
#endif // APP_H
main.qml:
import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.11
import QtQuick.Window 2.11
Window {
id: window
width: 1000
height: 1000
visible: true
Shortcut {
sequence: "F5"
onActivated: {
window.close()
app.loadQml()
}
}
}