//CustomWindow.h
class CustomWindow : public QQuickWindow {
//Custom QQuick window used in 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 connectToBasic
(QObject *object
= nullptr,
const QUrl &url
= QUrl(""));
private:
};
//** windowLoader.cpp **//
}
//This function will be called only one time during launch
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")));
m_iMainWindowIndex = 0;
m_iBasicWindowIndex = 1;
} 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")));
m_iMainWindowIndex = 1;
m_iBasicWindowIndex = 0;
}
}
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() {
if(loadQMlEngine.rootObjects().size() <= 2) {
disconnect
(&loadQMlEngine,
SIGNAL(objectCreated
(QObject *,
const QUrl &)),
this,
SLOT(connectToBasic
(QObject *,
const QUrl &)));
}
if(loadQMlEngine.rootObjects().size() < 2) {
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
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")));
} else {
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"show");
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"hide");
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",true);
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
}
}
void WindowLoader::loadMainWindow() {
if(loadQMlEngine.rootObjects().size() <= 2) {
disconnect
(&loadQMlEngine,
SIGNAL(objectCreated
(QObject *,
const QUrl &)),
this,
SLOT(connectToMain
(QObject *,
const QUrl &)));
}
if(loadQMlEngine.rootObjects().size() < 2) {
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
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 {
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"hide");
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"show");
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",true);
}
}
//** main.cpp **//
int main( int argc, char *argv[] ) {
qmlRegisterType<CustomWindow>("CNQml", 1, 0, "CustomWindow");
WindowLoader root;
root.loadWindow();
return app.exec();
}
//*********** QML Code ************
// ** mainWindow.qml **//
CustomWindow {
visible: true
width: 1200
height: 800
title: qsTr("MainWindow")
signal switchToBasicSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToBasicSignal()
}
}
}
}
//** basic.qml **//
CustomWindow {
visible: true
width: 640
height: 480
title: qsTr("basicwindow")
signal switchToMainSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToMainSignal()
}
}
}
}
//CustomWindow.h
class CustomWindow : public QQuickWindow {
//Custom QQuick window used in 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) {
}
//This function will be called only one time during launch
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")));
m_iMainWindowIndex = 0;
m_iBasicWindowIndex = 1;
} 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")));
m_iMainWindowIndex = 1;
m_iBasicWindowIndex = 0;
}
}
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() {
if(loadQMlEngine.rootObjects().size() <= 2) {
disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToBasic(QObject *, const QUrl &)));
}
if(loadQMlEngine.rootObjects().size() < 2) {
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
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")));
} else {
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"show");
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"hide");
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",true);
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",false);
}
}
void WindowLoader::loadMainWindow() {
if(loadQMlEngine.rootObjects().size() <= 2) {
disconnect(&loadQMlEngine,SIGNAL(objectCreated(QObject *, const QUrl &)),this,SLOT(connectToMain(QObject *, const QUrl &)));
}
if(loadQMlEngine.rootObjects().size() < 2) {
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
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 {
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iBasicWindowIndex],"hide");
//QMetaObject::invokeMethod(loadQMlEngine.rootObjects()[m_iMainWindowIndex],"show");
loadQMlEngine.rootObjects()[m_iBasicWindowIndex]->setProperty("visible",false);
loadQMlEngine.rootObjects()[m_iMainWindowIndex]->setProperty("visible",true);
}
}
//** main.cpp **//
int main( int argc, char *argv[] ) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<CustomWindow>("CNQml", 1, 0, "CustomWindow");
WindowLoader root;
root.loadWindow();
return app.exec();
}
//*********** QML Code ************
// ** mainWindow.qml **//
CustomWindow {
visible: true
width: 1200
height: 800
title: qsTr("MainWindow")
signal switchToBasicSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToBasicSignal()
}
}
}
}
//** basic.qml **//
CustomWindow {
visible: true
width: 640
height: 480
title: qsTr("basicwindow")
signal switchToMainSignal()
Rectangle {
anchors.fill: parent
MouseArea{
anchors.fill: parent
onClicked: {
switchToMainSignal()
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks