PDA

View Full Version : Subclassed QItemDelegate::sizeHint and is the item selected?



Lykurg
25th September 2008, 11:24
Hi,

I have an subclassed QItemDelegate to draw my own content to a QListView. In the sizeHint function I need to know if the item is selected. Unfortunately the given QStyleOptionViewItem doesn't hold this information. The only way I see is the selection model of the parent QListView. But how I get a pointer the the QListView out of the "QItemDelegate::sizeHint()" with the given QModelIndex or QStyleOptionViewItem?


Thanks,

Lykurg

caduel
25th September 2008, 11:57
isn't QStyleOptionViewItem::state & QStyle::State_Selected set?

Lykurg
25th September 2008, 13:25
isn't QStyleOptionViewItem::state & QStyle::State_Selected set?

No, that's the point. In my paint function it is set:
QStyleOption( SO_ViewItem , LeftToRight , QStyle::State( "Active | Enabled | HasFocus | Selected" ) , QRect(0,0 778x100) )
but in the sizeHint function it is only
QStyleOption( SO_ViewItem , LeftToRight , QStyle::State( "Active | Enabled" ) , QRect(0,0 100x300) )

caduel
25th September 2008, 13:40
the idea is probably that the size should not change when an item is selected.
(which is reasonable: otherwise you get a kind of "wobbling" effect as things have to be relayouted all the time just because you selected a different item...)

Lykurg
25th September 2008, 13:54
But this is exactly what I want. A bigger size for selected items. And I think sizeHint is requested when the item gets selected. For example I have three items and when I hide the window, sizeHint debug says:

sizeHint QStyleOption( SO_ViewItem , LeftToRight , QStyle::State( "Enabled" ) , QRect(0,0 100x300) ) 0
sizeHint QStyleOption( SO_ViewItem , LeftToRight , QStyle::State( "Enabled" ) , QRect(0,0 100x300) ) 1
sizeHint QStyleOption( SO_ViewItem , LeftToRight , QStyle::State( "Enabled" ) , QRect(0,0 100x300) ) 2

The last number is item index. So for all items sizeHint is requested, but item 0, which is selected, doesn't show the selection state in QStyle::State :-(

Lykurg