Solved How do I initialize QStyleOptionViewItem from an index?
-
So, I have a custom delegate QStyledItemDelegate, whose paint method takes: painter, option and index. I want to generate some scrolling information based on these values, more precisely, using the option and the index. But I want to do it in QListView which uses the delegate and not in the paint method itself. To that end, I need to somehow generate the right option for a given index, just like the one which is passed to the paint method. How can I do that?
-
Hi,
Can you explain more precisely what you are trying to achieve ?
It's not very clear what you want to do within the QListView. -
@SGaist Bascially, I want to make multiple anchor points (or a scroll grid) for an item, so as to avoid situations when the stuff inside an item is shown only partially (at the top of the widget). To generate the anchor points, I need to know things like
option.fontMetrics
.I figured
viewOptions()
produces thefontMetrics
I need. Butoption
should also contain some index-specific info, right? Not that I can think of a use for that right now, but I am curious if that would be possible to obtain should the need arise. -
Can you show a picture of what it should look like ?
-
@SGaist Sure,
So, basically, I never want text to be cut like this when scrolling at the top of the widget:
Only this is allowed after a scroll:
or this
Everything is working fine already, it wasn't hard to do. At this point, I am just curious if there's a way to somehow get
option
so that's it's just the same as passed toQStyledItemDelegate::paint
.QListView::viewOptions()
is a start, but it doesn't contain any index-specific info. I know I can getoption.rect
info withQListView::rectForIndex
. But I am not sure ifrect
is all there's to it (I don't know which other fields are index specific). Again, this is for curiousity. AFAIK viewOptions + rectForIndex give me all the info I need for now, but some sort ofoptionForIndex
would just be cleaner and nicer to have. -
The option is initialized in QStyledItemDelegate::initStyleOption.
-
@SGaist Great, thanks, that's what I was looking for!