Unsolved How to change color of the window dynamically?
-
@dheerendra : isMinimized is a property declared in the MainWindow.qml. It's value is set to true when user clicks on minimize button on the customized titlebar. So when mainWindow object is constructed, and when user clicks on minimize button, I need maunwindow's color to be transparent. And red color when it is restored back.
@fcarney : It's reproducing in my code base, which is based on Qt 5.12. Let me try getting minimal code to reproduce this issue.
-
@rakeshthp Are you able to see the problem with sample reproducible program ? If yes, can you send the same ?
-
Hi @dheerendra , @fcarney
Here is the sample reproducible program. In main.qml, there are four set of statements to set color to the window. All four can be tested one by one. Once you run this, there will be a small black colored rectangle. On clicking that rectangle the background color of the window changes. Below are the observations
1. color: "transparent"
Works fine, I see transparent window
2. color: isMinimized ? "red" : "green"
This also works fine. I see red and green colors alternatively
3. color: isMinimized ? "transparent" : "green"
This one fails: I don't see transparent background while toggling color
4. color: !isMinimized ? "transparent" : "green"
Works fine: I see expected colors while toggling.
Not sure why #3 is failing, which is exactly we are facing issue in our project.
So what do you guys think, is missing here or wrong here?
NOTE: This is tested with Qt 5.12.5 with MSVC2015 on Windows 10 Pro, with Intel(R) Core(TM) i7-6699U CPU@2.60GHz
Let me know if you guys need any other info.
Thanks
-
main.qml
import QtQuick 2.12 import QtQuick.Window 2.12 Window { property bool isMinimized: false flags: Qt.Window | Qt.FramelessWindowHint | Qt.WA_TranslucentBackground visible: true width: 640 height: 480 title: qsTr("Hello World") // color: "transparent" // color: isMinimized ? "red" : "green" // color: isMinimized ? "transparent" : "green" color: !isMinimized ? "transparent" : "green" Rectangle { color: "transparent" height: parent.height width: parent.width anchors.fill: parent border.width: 2 border.color: "blue" Rectangle { height: parent.height * 0.1 width: parent.width * 0.02 color: "black" anchors { left: parent.left verticalCenter: parent.verticalCenter } MouseArea { anchors.fill: parent onClicked: { isMinimized = !isMinimized console.log("Clicked") } } } } }
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickWindow> #include <QPalette> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software); QGuiApplication app(argc, argv); QPalette palette; palette.setColor(QPalette::Base, Qt::transparent); app.setPalette(palette); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); }
.pro file
QT += quick CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Refer to the documentation for the # deprecated API to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
I don't see a issue. I tried your same program. It works fine. I tried with QtQuick 2.14.
Which platform are you trying ?
-
@dheerendra did you try all four ways of setting colour to the window? Three of them are commented.
-
yes. All tried.
-
That's really strange, as I am seeing it in the configuration mentioned above. Are you running it on Windows?
-
@dheerendra : This is tested with Qt 5.12.5 with MSVC2015 on Windows 10 Pro, with Intel(R) Core(TM) i7-6699U CPU@2.60GHz
@fcarney : Do you also see it working fine?
-
Attached screenshot of issue what I am noticing with this piece of code.
Thanks in advance
-
Do you think it may be bug in QtQuick 2.12 ?