我需要启动 2 个不同的 Qml 文件“mainwindow.qml”和“basic.qml”,而两者都是相互独立的。
最初我需要基于标志 bSettingMainWindow 启动 qml 窗口,基于此我将启动任何一个 qml 文件。
启动后,我需要随时在这 2 个 qml 文件之间切换。它应该像加载和卸载“mainwindow.qml”和“basic.qml”,基于用户操作。因为因为内存问题,我需要一次加载它们中的任何一个。而且我不想玩可见的真假。
而从下面的代码中,我可以加载基于标志 bSettingMainWindow 的任何一个 qml 文件。并且在加载后我可以切换到另一个 qml 文件窗口。
假设我首先在 Mainwindow.qml 中启动,然后我在 mainwindow.qml 的矩形上单击鼠标按钮并移动到 basic.qml,现在如果我单击 basic.qml 的矩形,它不会切换到 mainwindow.qml。反之亦然。 只有一次我可以切换这两个黑白。我想多次切换。 以下是代码,请提供您的答案
//** 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()
}
}
}
}