Important: Please read the Qt Code of Conduct -

Update listview model from delegate paint event

  • I have a QListView where I custom draw the iconview using a delegate. I need to save a QRect containing the coordinates of part of an iconview item that can be used later to translate coordinates from a mouse click event on the thumbnail. I am having difficulties overcoming the const status.

    Here is a code snippet from the delegate:

    void ThumbViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                               const QModelIndex &index) const
        // define iconRect coordinates
        // save iconRect to model
        index.model()->setData(index, iconRect, Qt::UserRole+5);
        // this generates error
        // qt cannot convert 'this' pointer from 'const QAbstractItemModel' to 'QAbstractItemModel &'

    How do I update the model?

    Thanks in advance.

  • Lifetime Qt Champion


    The paint event is not the place to do it at all, you should rather take a look at the mouse events related methods of QListView.

  • @SGaist

    Thanks. I would like to do it that way, but the paint event is the only place where the interior geometry of the iconview is defined. I think I have solved the problem with a signal/slot.

  • Lifetime Qt Champion

    You're welcome !

    QAbstractItemView::visualRect should give you that information.

  • @SGaist

    No, it does not I am afraid. In my delegate I want the location of the icon, which I size and place within the visualRect. The visualRect only gives the outer dimensions.

Log in to reply