I have requirement to launch 2 different Qml files "mainwindow.qml" and "basic.qml" ,wheras both are independent of each other.
Initially i need to launch qml window based on flag bSettingMainWindow, based on this i will be launching any one of the qml file.
After launching I need to switch between these 2 qml files anytime. It should be like loading and unloading of "mainwindow.qml" and "basic.qml", based on the user action. Since because memory concern , i need load any of them at a time. and i dont want to play with visible true or false.
whereas from this below code I can able to load any one of the qml file based on the flag bSettingMainWindow. and also after loading i can able to switch to another qml file window.
Assume first I have launched in Mainwindow.qml then i clicked the mouse button over Rectangle of mainwindow.qml and moved to basic.qml and now if i click on the Rectangle of basic.qml it is not switching to mainwindow.qml. and vice versa. Only one time i can able to switch b/w these 2 . I want to switch multiple times. Below is the code , requesting to please provide your answers
//** 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();
}
// ** 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()
}
}
}
}