[PyQt] How do I draw a styled focus rectangle in a QStyledItemDelegate?

Benno Dielmann lists at benno-dielmann.de
Tue Jul 15 00:10:26 BST 2008


Hi all, 

I've got a QStyledItemDelegate subclass which implements paint() to draw 
the contents of some QTableView cells. How do I make it paint a focus 
rectangle if one of those cells has got focus? I tried this:

class MyDelegate(QStyledItemDelegate):
  ...
    def paint(self, painter, option, index):
      ...
       painter.save()
       if option.state & QStyle.State_HasFocus:
           self.parent().style().drawPrimitive(QStyle.PE_FrameFocusRect, option, painter)
      ...
       painter.restore()

but this simply does nothing. No errors, no focus frame. I just want 
the QStyle system to somehow paint the usual focus frame if one of my 
custom painted cells have focus. The QStyle documentation tells me to 
create a QStyleOptionFocusRect and to use initFrom(). But initFrom() 
needs a QWidget which I don't have in this case. 

I just don't get it. 

What's the usual way to get focus frames in QTableView cells painted by custom delegates?

Thanks very much for any answer, 

Benno. 


More information about the PyQt mailing list