user11766756
user11766756

Reputation:

How to add background color to a specific dropdown from combobox in pyqt4

cbo.setStyleSheet('background-color: rgb(205,92,92)')

This adds background to the entire combobox. I would like to add background color to certain rows of a combobox. Thanks

Upvotes: 3

Views: 1609

Answers (2)

user11766756
user11766756

Reputation:

To add color to a specific index row of a cbo (combobox) in pyqt4:

cbo.model().item(index).setForeground(QtGui.QColor("green"))
cbo.model().item(index).setBackground(QtGui.QColor("red"))

To display color of the text of cbo row clicked when cbo closes:

color = cbo.model().item(index).foreground().color().getRgb() #index is index that was clicked
cbo.setStyleSheet("QComboBox:editable{{ color: rgb{} }}".format(color))

If it needed to be hardcorded then :

cbo.setStyleSheet("QComboBox:editable{{ color: {} }}".format('red'))

Upvotes: 2

eyllanesc
eyllanesc

Reputation: 244242

Qt has an internal model (QStandardItemModel) that allows you to set the color of each item through setItemData():

index = 2
cbo.setItemData(index, QtGui.QColor(205, 92, 92), QtCore.Qt.BackgroundRole)

The visible element of the QComboBox is not part of the dropdown so they will not share the default color, a possible solution is to use the model information to change the QPalette:

class ComboBox(QtGui.QComboBox):
    def __init__(self, parent=None):
        super(ComboBox, self).__init__(parent)

        self.currentIndexChanged[int].connect(self._on_currentIndexChanged)
        self.model().dataChanged.connect(self._on_currentIndexChanged)


    def _on_currentIndexChanged(self):
        index = self.currentIndex()
        pal = self.palette()
        color = cbo.itemData(index, QtCore.Qt.BackgroundRole)
        if color is None:
            color = pal.color(QtGui.QPalette.Button)
            cbo.setItemData(index, color, QtCore.Qt.BackgroundRole)
        else:
            pal.setColor(QtGui.QPalette.Button, color)
            self.setPalette(pal)

Example:

import random

from PyQt4 import QtCore, QtGui


class ComboBox(QtGui.QComboBox):
    def __init__(self, parent=None):
        super(ComboBox, self).__init__(parent)

        self.currentIndexChanged[int].connect(self._on_currentIndexChanged)
        self.model().dataChanged.connect(self._on_currentIndexChanged)


    def _on_currentIndexChanged(self):
        index = self.currentIndex()
        pal = self.palette()
        color = cbo.itemData(index, QtCore.Qt.BackgroundRole)
        if color is None:
            color = pal.color(QtGui.QPalette.Button)
            cbo.setItemData(index, color, QtCore.Qt.BackgroundRole)
        else:
            pal.setColor(QtGui.QPalette.Button, color)
            self.setPalette(pal)


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()

    lay = QtGui.QVBoxLayout(w)

    cbo = ComboBox()
    cbo.addItems(["one", "two", "three", "four", "five"])

    indexes = random.sample(range(cbo.count()), 3)
    for index in indexes:
        color = QtGui.QColor(*random.sample(range(255), 3))
        cbo.setItemData(index, color, QtCore.Qt.BackgroundRole)

    lay.addWidget(cbo)
    lay.addWidget(QtGui.QTextEdit())
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions