tags:

views:

557

answers:

1

So here's the story:

I have a QListview that uses a QSqlQueryModel to fill it up. Because some items should display in bold based on the value of a hidden column of the model, I decided to make my own custom delegate. I'm using PyQT 4.5.4 and thus inheriting from QStyledItemDelegate is the way to go according to the docs. I got it working but there are some problems with it.

Here's my solution:

class TypeSoortDelegate(QStyledItemDelegate):

    def paint(self, painter, option, index):
        model = index.model()
        record = model.record(index.row())
        value= record.value(2).toPyObject()
        if value:
            painter.save()
            # change the back- and foreground colors
            # if the item is selected
            if option.state & QStyle.State_Selected:
                painter.setPen(QPen(Qt.NoPen))
                painter.setBrush(QApplication.palette().highlight())
                painter.drawRect(option.rect)
                painter.restore()
                painter.save()
                font = painter.font
                pen = painter.pen()
                pen.setColor(QApplication.palette().color(QPalette.HighlightedText))
                painter.setPen(pen)
            else:
                painter.setPen(QPen(Qt.black))

            # set text bold
            font = painter.font()
            font.setWeight(QFont.Bold)
            painter.setFont(font)
            text = record.value(1).toPyObject()
            painter.drawText(option.rect, Qt.AlignLeft, text)

            painter.restore()
        else:
            QStyledItemDelegate.paint(self, painter, option, index)

The problems I'm facing now:

  1. the normal (not bold) items are slightly indented (a few pixels). This is probably some default behaviour. I could indent my item in bold also, but what happens then under a different platform?
  2. Normally when I select items there is a small border with a dotted line around it (default Windows thing?). Here also I could draw it, but I want to stay as native as possible.

Now the question:

Is there another way to create a custom delegate that only changes the font weight when some condition is met and leaves all the rest untouched?

I also tried:

if value:
    font = painter.font()
    font.setWeight(QFont.Bold)
    painter.setFont(font)
QStyledItemDelegate.paint(self, painter, option, index)

But that doesn't seem to affect the looks at all. No error, just default behaviour, and no bold items.

All suggestions welcome!

+1  A: 

I've not tested this, but I think you can do:

class TypeSoortDelegate(QStyledItemDelegate):

def paint(self, painter, option, index):
    get value...
    if value:
        option.font.setWeight(QFont.Bold)

    QStyledItemDelegate.paint(self, painter, option, index)
Gary van der Merwe