How to change the QCombobox highlight colour in PyQt

3.6k views Asked by At

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.

2

There are 2 answers

7
ekhumoro On BEST ANSWER

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_()
0
Alan Zheng On

I'd like to add that even though it might seem obvious to go set "selection-background-color" for QComboBox, this property in fact belongs to QWidget. So if you need to be explicit, you can do something like this combo.setStyleSheet('QWidget{selection-background-color: rgb(168,168,168);}')