Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?


  • Lifetime Qt Champion

    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 the fontMetrics I need. But option 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.


  • Lifetime Qt Champion

    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:

    2020_05_08__04_46_15__820562542.png

    Only this is allowed after a scroll:

    2020_05_08__04_48_10__260020934.png

    or this

    2020_05_08__05_11_36__221145242.png

    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 to QStyledItemDelegate::paint. QListView::viewOptions() is a start, but it doesn't contain any index-specific info. I know I can get option.rect info with QListView::rectForIndex. But I am not sure if rect 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 of optionForIndex would just be cleaner and nicer to have.


  • Lifetime Qt Champion

    The option is initialized in QStyledItemDelegate::initStyleOption.



  • @SGaist Great, thanks, that's what I was looking for!


Log in to reply