2

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.

Kermit
  • 329
  • 6
  • 19
  • The palette does not change things in-place, so you need to use `setPalette(palette)`. – ekhumoro Oct 08 '17 at 18:25
  • @ekhumoro sorry, this is already done and it still does not change the colour, only the highlight colour of the box(first picture). Will update code in question. – Kermit Oct 08 '17 at 18:55
  • Works fine for me on linux. If you're on windows or osx, the palette is sometimes ignored. You might need to use a stylesheet. – ekhumoro Oct 08 '17 at 20:20
  • @ekhumoro must be that. How would I go about changing this using the stylesheet? I know how to edit style sheets just not sure how to edit the highlight colour. Cheers – Kermit Oct 09 '17 at 11:40

2 Answers2

3

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_()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Tried this before and it sadly did not solve the problem. – Kermit Oct 09 '17 at 14:19
  • @Kermit. Do you mean it doesn't work with your own code, or that the code I showed in my answer doesn't work? – ekhumoro Oct 09 '17 at 14:24
  • @ekhumuro I meant adding "selection-background-color: rgb(168,168,168)" to the stylesheet of the combobox not working. – Kermit Oct 09 '17 at 14:25
  • @Kermit. Could you please run the example script shown in my updated answer, and confirm that it doesn't work? – ekhumoro Oct 09 '17 at 14:42
  • @ekhumuro odd, your exact code now works for the specific combobox. I will have to edit my code to this somehow. For now this answers my Q, cheers – Kermit Oct 09 '17 at 14:42
  • @Kermit. Have you set a completer on the combobox, or set a custom view? – ekhumoro Oct 09 '17 at 14:43
  • @ekhumuro solved myself with your help. Realised I set the whole window stylesheet and it was effecting all the widgets inside the window. I will still leave the question and solution up in case someone else finds it helpful, cheers. – Kermit Oct 09 '17 at 14:59
0

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);}')

Dharman
  • 30,962
  • 25
  • 85
  • 135