python-3.xuser-interfacepyqt5qpalette

How to change the QCombobox highlight colour in PyQt


I am having trouble changing the highlight colour of a QCombobox in PyQt. I have managed to change the highlight colour of the actual input box but when the drop down appears it is still blue. The following images shows what is exactly happening. The palette method works on Linux but not on Windows (what I am currently using). I used PyQt palette:

    brush = QtGui.QBrush(QtGui.QColor(168, 168, 168))
    brush.setStyle(QtCore.Qt.SolidPattern)
    palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Highlight, brush)
    self.comboBox_7.setPalette(palette)

Here I managed to change the highlight colour to grey of the actual box:

image1

but here the drop down highlight is still blue:

image2

all help appreciated.


Solution

  • According to the Qt docs, the palette may not always work on some platforms:

    Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows XP, Windows Vista, and the macOS styles.

    The Qt Style Sheets Overview suggests that a stylesheet should work where the palette doesn't. I cannot test this myself on anything other than Linux, but the following seems to work okay:

    from PyQt5 import QtWidgets
    app = QtWidgets.QApplication([''])
    combo = QtWidgets.QComboBox()
    combo.addItems('One Two Three'.split())
    combo.setStyleSheet('selection-background-color: rgb(168,168,168)')
    combo.show()
    app.exec_()