Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using setBackground correctly

Hopefully an easy one for you...

I've been trying to set the background color of a QtListWidgetItem, but I'm not having much luck - this is probably because I'm not using the QListWidgetItem correctly... In my test code I can set the foreground of every third item in the list, but setting the background seems to have no effect. Can anyone spot my stupid error?

I've tested this out with both PyQt4 and PySide on Qt 4.7

Thanks, Dan

import sys
from PySide import QtCore, QtGui

class List(QtGui.QListWidget):
    def __init__(self):
        super(List, self).__init__()
        self.populate()

    def populate(self):
        for i in range(32):
            item = QtGui.QListWidgetItem(self)
            item.setText('%d'%i)

            if i % 3 == 0:
                brush = QtGui.QBrush()
                brush.setColor(QtGui.QColor('red'))
                item.setBackground(brush)

            if i % 3 == 1:
                brush = QtGui.QBrush()
                brush.setColor(QtGui.QColor('blue'))
                item.setForeground(brush)

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    listw = List()
    listw.show()

    sys.exit(app.exec_())
like image 967
danodonovan Avatar asked Dec 05 '11 17:12

danodonovan


1 Answers

You don't really need a brush. Just use a QColor:

item.setBackground(QtGui.QColor('red'))
like image 170
Blender Avatar answered Nov 19 '22 22:11

Blender