Luca
Luca

Reputation: 10996

How do I change the background color of a QtQuick Window?

I am attempting to create a simple QML application. The QML file is very simple:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
   id: root
   objectName: "window"
   visible: true
   width: 800
   height: 480
   color: "#16FF16"
}

I'm using the following C++ code:

QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl("qrc:/qml/main.qml"));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
//window->setFlags(Qt::Widget|Qt::FramelessWindowHint);
window->show();
return app.exec();

This shows the window fine and the background color is a bit green as expected. However, as soon as I uncomment the line:

window->setFlags(Qt::Widget|Qt::FramelessWindowHint);

The window always shows up as black now!

How can I set this frameless property without having these unintended side-effects?

Upvotes: 3

Views: 8352

Answers (1)

iBelieve
iBelieve

Reputation: 1544

I'm not sure why the window is showing up black for you. Perhaps it is the because of the Qt::Widget flag.

However, this works and is actually simpler as it keeps all your UI code in QML:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
   id: root
   objectName: "window"
   visible: true
   width: 800
   height: 480
   color: "#16FF16"

   // Set the flag directly from QML
   flags: Qt.FramelessWindowHint
}

Upvotes: 5

Related Questions