[PyQt] Override QStyledItemDelegate.initStyleOption not working?
Andreas Pakulat
apaku at gmx.de
Wed Jan 11 17:59:11 GMT 2012
On 11.01.12 14:47:25, Phil Thompson wrote:
> On Tue, 10 Jan 2012 21:19:18 +0100, Andreas Pakulat <apaku at gmx.de> wrote:
> > On 10.01.12 10:19:13, Phil Thompson wrote:
> >> On Tue, 10 Jan 2012 00:11:49 +0100, Andreas Pakulat <apaku at gmx.de>
> wrote:
> >> > Hi,
> >> >
> >> > I'd like to customize the display of a model via a
> styleditemdelegate,
> >> > in particular I want to add a decorating icon depending on some
> custom
> >> > role from the model.
> >> >
> >> > As far as I can see the easiest way to do this would be overriding
> >> > initStyleOption since then I can set the icon and icon-size directly
> on
> >> > the option and leave the rest to the standard paint etc. methods.
> >> >
> >> > Unfortunately it seems this does not work in PyQt, the code is
> executed
> >> > properly but the icon does not show up in the itemview. Since the
> >> > function in C++ takes a pointer to the option-parameter I wonder
> wether
> >> > this is simply not possible with PyQt or wether I need to go hunting
> >> > for
> >> > the bug elsewhere?
> >> >
> >> > Using Qt 4.7.4, sip 4.13 and PyQt 4.8.6
> >>
> >> Are you keeping a reference to the option - if not see if this makes a
> >> difference.
> >
> > Unfortunately no, I've written up a small example for what I'm trying to
> > do and attached it.
> >
> > I've also tried overriding paint(), calling initStyleOption there, then
> > calling the parent paint() function and the icon is set on the option
> > parameter in between, but the icon is not painted in the gui...
>
> If you implement your code to set the icon in Qt's implementation of
> QStyledItemDelegate::paint() itself then the results are the same.
> Therefore I don't think it is a PyQt problem. That would suggest either a
> Qt bug or a it-doesn't-work-the-way-you-think-it-does problem.
Its actually a "you forgot to copy something from the C++
implementation" issue :)
For those finding this in the archives: QStyleOptionViewItemV2 has a
features member which can contains a HasDecoration feature. Setting that
in the initStyleOption function makes the icons show up, so apparently
the style checks that feature before even looking at the icon or
decorationSize - at least my style here does that (couldn't spot this in
QCommonStyle).
Andreas
More information about the PyQt
mailing list