Qt World Summit: Submit your Presentation

How to use standard platform delegate for custom QAbstractItemView object

  • Hi,
    I have reimplemented a QAbstractItemView viewer that has more space for the focused item than for regular items, in order to make room for a (moving) toolbar. I have also implemented paintevent() method to render items.
    The whole thing works, but my custom rendering is quite 'primitive'.

    I'd like to have the same delegate that regular QListView uses, with platform highlight for hovering mouse and focused item, (I work on windows Vista). As far as I understand, using a delegate is useful for this purpose: having rendering logic independent from View behavior, once you have selected the correct 'state' and 'feature' flags for QStyleOptionViewItem object. However default paint behavior of QStyledItemDelegate produces a very different (worse) result.

    How can I bind platform style to my custom QAbstractItemView ?

    Thank you

  • Moderators

    as the same says QStyledItemDelegate is the correct way to go.
    If you use stylesheets it uses this style to render the items. If not the system style is used.
    Maybe you can be more specific about the "very different (worse) result"?

  • The worse result is:

    • selected items have a light gray background, hardly different from screen background;
    • hovering mouse has no effect
    • the gray background is only behind text, not behind the whole item area.

    What I expected is a light blue background when hovering mouse a blue background on selected items, (classic Windows Vista trees).
    Do I have to explicitly set a stylesheet? If so, is there a "Windows template" ?

    Thanks for your reply.

  • Lifetime Qt Champion


    How did you implement your viewer ?

  • @SGaist:
    The viewer inherits from "QAbstractItemView" and in paintEvent method set the field of a QStyleOptionViewItem, (State_HasFocus State_Selected State_Enabled State_MouseOver), then I call paint method of the delegate.
    The delegate inherits from QStyledItemDelegate and it's paint method has only one instruction: the call to parent function
    QStyledItemDelegate::paint( painter, option, index);

Log in to reply