PDA

View Full Version : QQuickWindow is not coming on top, if I make the visible false QQuickWindow as true



gowreesh
7th July 2020, 09:10
I have designed this below code like if the user launches the application for the first time , then the application will load Mainwindow.qml or basic.qml based on the flag "bSettingMainWindow" . Next time based on the switch button click from application will load another window (other than first one) and making the first launched qml window visible false. Once both of them launched once, then 2 windows will play with visibility true or false on click of switch button or signal is emitted.

This below code is working perfectly fine except the window is not coming on top after making visible true. I have tried using raise() and activatewindow() even after that i am facing the same issue.

Requesting to please provide any solution for to make the window come on top when i play with visibility. Currently this application window is settling behind some other application like for example outlook, after making visible true.

Note: I have asked this question in my previous thread, But i didnt got any solution for this question(yes i have got solution for my other questions) so I am asking again with new thread since the previous thread has so many other contents which are not required. Click here (https://www.qtcentre.org/threads/70942-Dynamically-loading-and-unloading-2-qml-files-from-cpp?p=308100#post308100)to view

Please find the code below


//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.rootObject s()[m_iBasicWindowIndex],"show");
//QMetaObject::invokeMethod(loadQMlEngine.rootObject s()[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.rootObject s()[m_iBasicWindowIndex],"hide");
//QMetaObject::invokeMethod(loadQMlEngine.rootObject s()[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_EnableHighDpiSca ling);
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()
}
}
}
}