2

I'm trying to change the color of the combobox background. I want it to be white, but I can't make it have any color different from gray. The picture on top is what I'm getting, and the one on the bottom is what I'm after.

enter image description here

Here's my code:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
   def __init__(self, parent = None):
      super(combodemo, self).__init__(parent)

      layout = QtWidgets.QHBoxLayout()
      self.cb = QtWidgets.QComboBox()
      self.cb.addItem("1")
      self.cb.addItem("2")
      pal = self.cb.palette()
      pal.setColor(self.cb.backgroundRole(),QtGui.QColor(255,255,255))
      self.cb.setPalette(pal)
      self.cb.setAutoFillBackground(True)

      layout.addWidget(self.cb)
      self.setLayout(layout)

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   ex = combodemo()
   ex.show()
   app.exec_()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tendero
  • 1,136
  • 2
  • 19
  • 34

2 Answers2

3

You have to change the color of the QPalette::Button role:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super(combodemo, self).__init__(parent)

        layout = QtWidgets.QHBoxLayout(self)

        self.cb = QtWidgets.QComboBox()
        self.cb.addItems(["1", "2"])

        pal = self.cb.palette()
        pal.setColor(QtGui.QPalette.Button, QtGui.QColor(255,255,255))
        self.cb.setPalette(pal)

        layout.addWidget(self.cb)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ex = combodemo()
    ex.show()
    sys.exit(app.exec_())

In Windows you need:

app.setStyle("fusion")
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Note that some Qt styles ('Macintosh' and 'Windows') do not use the palette for everything. This solution unfortunately doesn't work on OSX (I can't test on Windows 10 now). You have to set the Application style to 'Fusion' by adding `app.setStyle('Fusion')` to make sure it works. This will make your application have the same look as under Linux. Or you will have to resort to style-sheets. – titusjan Aug 16 '18 at 14:18
  • 1
    @Tendero use `app.setStyle("fusion")` – eyllanesc Aug 16 '18 at 14:18
2

To avoid changing the style for the entire application, I used a style sheet, per How to set background color of QComboBox button?.

Note that, as stated in the post, this styles "the whole drop-down to mimic the native look, but it's not nice to do and not robust (and not portable)." However, since changing the style of the application has larger implications, this approach works for my purposes.

Here's my style sheet:

QComboBox QAbstractItemView {
  border: 1px solid grey;
  background: white;
  selection-background-color: blue;
}
QComboBox {
  background: red;
}

and code:

from PyQt5 import QtWidgets, QtGui

class combodemo(QtWidgets.QWidget):
   def __init__(self, parent = None):
      super(combodemo, self).__init__(parent)

      layout = QtWidgets.QHBoxLayout()
      self.cb = QtWidgets.QComboBox()
      self.cb.addItem("1")
      self.cb.addItem("2")

      cbstyle =  "QComboBox QAbstractItemView {"
      cbstyle += " border: 1px solid grey;"
      cbstyle += " background: white;"
      cbstyle += " selection-background-color: blue;"
      cbstyle += " }"
      cbstyle += " QComboBox {"
      cbstyle += " background: white;"
      cbstyle += "}"
      self.cb.setStyleSheet(cbstyle)

      layout.addWidget(self.cb)
      self.setLayout(layout)

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   ex = combodemo()
   ex.show()
   app.exec_()
mherzog
  • 1,085
  • 1
  • 12
  • 24