//** windowLoader.hpp **//
class WindowLoader
: public QObject{ Q_OBJECT
QQmlApplicationEngine loadQMlEngine;
public:
explicit WindowLoader
(QObject * parent
= 0);
void loadWindow();
public slots:
void loadMainWindow();
void loadBasicWindow();
void connectToBasic
(QObject *object
= nullptr,
const QUrl &url
= QUrl(""));
private:
};
//** windowLoader.cpp **//
}
void WindowLoader::loadWindow() {
if(bSettingMainWindow){ //this will be from a internal flag, this check is only one time during launch
connect(&loadQMlEngine,
SIGNAL(objectCreated
(QObject *,
const QUrl &)),
this,
SLOT(connectToBasic
(QObject *,
const QUrl &)),Qt
::QueuedConnection);
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.
load(QUrl(QStringLiteral
("qrc:/Qml/mainWindow.qml")));
} else {
connect(&loadQMlEngine,
SIGNAL(objectCreated
(QObject *,
const QUrl &)),
this,
SLOT(connectToMain
(QObject *,
const QUrl &)),Qt
::QueuedConnection);
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.
load(QUrl(QStringLiteral
("qrc:/Qml/basic.qml")));
}
}
void WindowLoader
::connectToBasic(QObject *object,
const QUrl &url
) { if(object){
connect(object, SIGNAL(switchToBasicSignal()), this, SLOT(loadBasicWindow()));
}
}
void WindowLoader
::connectToMain(QObject *object,
const QUrl &url
) { if(object){
connect(object, SIGNAL(switchToMainSignal()), this, SLOT(loadMainWindow()));
}
}
void WindowLoader::loadBasicWindow() {
loadQMlEngine.rootObjects()[0]->deleteLater();
loadQMlEngine.destroyed();
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.
load(QUrl(QStringLiteral
("qrc:/Qml/basic.qml")));
}
void WindowLoader::loadMainWindow() {
loadQMlEngine.rootObjects()[0]->deleteLater();
loadQMlEngine.destroyed();
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.
load(QUrl(QStringLiteral
("qrc:/Qml/mainWindow.qml")));
}
//** main.cpp **//
int main( int argc, char *argv[] ) {
WindowLoader root;
root.loadWindow();
return app.exec();
}
//** windowLoader.hpp **//
class WindowLoader : public QObject{
Q_OBJECT
QQmlApplicationEngine loadQMlEngine;
public:
explicit WindowLoader(QObject * parent = 0);
void loadWindow();
public slots:
void loadMainWindow();
void loadBasicWindow();
void connectToMain(QObject *object = nullptr, const QUrl &url = QUrl(""));
void connectToBasic(QObject *object = nullptr, const QUrl &url = QUrl(""));
private:
};
//** windowLoader.cpp **//
WindowLoader::WindowLoader(QObject *parent) : QObject(parent) {
}
void WindowLoader::loadWindow() {
if(bSettingMainWindow){ //this will be from a internal flag, this check is only one time during launch
connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)),Qt::QueuedConnection);
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
} else {
connect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)),Qt::QueuedConnection);
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
}
}
void WindowLoader::connectToBasic(QObject *object, const QUrl &url) {
if(object){
connect(object, SIGNAL(switchToBasicSignal()), this, SLOT(loadBasicWindow()));
}
}
void WindowLoader::connectToMain(QObject *object, const QUrl &url) {
if(object){
connect(object, SIGNAL(switchToMainSignal()), this, SLOT(loadMainWindow()));
}
}
void WindowLoader::loadBasicWindow() {
loadQMlEngine.rootObjects()[0]->deleteLater();
loadQMlEngine.destroyed();
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/basic.qml")));
}
void WindowLoader::loadMainWindow() {
loadQMlEngine.rootObjects()[0]->deleteLater();
loadQMlEngine.destroyed();
loadQMlEngine.rootContext()->setContextProperty( "interface", m_interface );
loadQMlEngine.load(QUrl(QStringLiteral("qrc:/Qml/mainWindow.qml")));
}
//** main.cpp **//
int main( int argc, char *argv[] ) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
WindowLoader root;
root.loadWindow();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
// ** mainWindow.qml **//
ApplicationWindow {
visible: true
width: 1200
height: 800
title: qsTr("MainWindow")
signal switchToBasicSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToBasicSignal()
}
}
}
}
//** basic.qml **//
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("basic")
signal switchToMainSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToMainSignal()
}
}
}
}
// ** mainWindow.qml **//
ApplicationWindow {
visible: true
width: 1200
height: 800
title: qsTr("MainWindow")
signal switchToBasicSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToBasicSignal()
}
}
}
}
//** basic.qml **//
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("basic")
signal switchToMainSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToMainSignal()
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks