How to change color of QMainWindow borders and title bar?

alphanumeric picture alphanumeric · Feb 12, 2015 · Viewed 16.7k times · Source

The QMainWindow below is assigned a dark-gray background-color using QSS. I would also like to change the color of the borders and the color of the title bar.

How to achieve control of the appearance of the QMainWindow borders and titlebar?

I would like to know how to change their colors and how to control the borders width and the title-bar's height.

enter image description here

from PyQt4.QtCore import *
from PyQt4.QtGui import *

appStyle="""
QMainWindow{
background-color: darkgray;
}
"""

class GUI(QMainWindow):
    def __init__(self):
        super(GUI, self).__init__()  
        self.setStyleSheet(appStyle)

if __name__ == '__main__': 
    if not QApplication.instance(): app=QApplication([])
    w=GUI() 
    w.setStyleSheet(appStyle)
    w.show() 
    w.raise_()

    sys.exit(app.exec_())

Answer

Trilarion picture Trilarion · Feb 12, 2015

To my knowledge you cannot (on Windows maybe with some special OS dependent calls) modify the borders and header of a top-level widget (real window on the desktop) in Qt because these are delivered from the operating system.

You can however, set the widget frameless and add borders for yourself.

Example:

from PySide import QtGui, QtCore

app = QtGui.QApplication([])
app.setStyleSheet('QMainWindow{background-color: darkgray;border: 1px solid black;}')

w = QtGui.QMainWindow()
w.setWindowFlags(QtCore.Qt.FramelessWindowHint)
w.show()

app.exec_()

And looks like

enter image description here

You see that unfortunately the usually header bar vanishes, so no dragging, no resizing, no closing or minimizing. This then all must be implemented yourself. See for example How can I handle events in the titlebar and change its color etc ?.