Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractItemDelegate.paint seems to be called with wrong option.rect
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemDelegate.paint seems to be called with wrong option.rect

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 335 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HalfTough
    wrote on last edited by
    #1

    I'm trying to have QListView to display some images in a list. Because I also want to add some custom look to those images, I'm using custom item delegate.

    Most of it is pretty standard. I have class inheriting from QAbstractItemDelegate and set object of this class as QListView's delegate.
    I want size of images adjust to the list widget, which can be resized by user.

    I implement sizeHint, so images scale with the widget

        def sizeHint(self, option: QStyleOptionViewItem, index: Union[QModelIndex, QPersistentModelIndex]) -> QSize:
            widget: QListView = option.widget
            image = index.model().data(index, Qt.DisplayRole)
            height = option.widget.height() - widget.spacing() * 2
            width = image.scaled_width(height)
            return QSize(width, height)
    

    and here's my paint

        def paint(self, painter: QPainter, option: QStyleOptionViewItem,
                  index: Union[QModelIndex, QPersistentModelIndex]) -> None:
            image: ImageInfo = index.model().data(index, Qt.DisplayRole)
            qimg = image.qimage(option.rect.size())  # gets image scaled to proper size
            painter.drawImage(option.rect, qimg)
    

    This code works fine as long I don't resize the view. When I do resize it, images scale, but don't change their position. Especially option.rect has proper width and height, but x always stays the same. So, if we resize widget, so it's bigger, images will grow, but stay on the same positions and in result start obscure each other. Any idea why this is happening and how I might be able to fix it?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      HalfTough
      wrote on last edited by
      #2

      I was able to find a hack. By making my model think it was updated I force update of positions passed to paint. Still, wish I'd found better solution.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Just thinking quickly, shouldn't you adjust the rect position based on the index row ?

        By the way, I think you should use QStyledItemDelegate as base class for your delegate since it's the default class used for the item views.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Just thinking quickly, shouldn't you adjust the rect position based on the index row ?

          By the way, I think you should use QStyledItemDelegate as base class for your delegate since it's the default class used for the item views.

          H Offline
          H Offline
          HalfTough
          wrote on last edited by
          #4

          @SGaist said in QAbstractItemDelegate.paint seems to be called with wrong option.rect:

          Just thinking quickly, shouldn't you adjust the rect position based on the index row ?

          In this case, images can have different aspect ratios, so I'd need ratio of each image before current one to calculate where to paint it. (It can be done, but seems to be going against design of delegate.)

          More importantly, values of rect seem to still affect how mouse events are handled, so even if I'd adjust paintings, selecting element's would work on offsets defined by rect and click event's wouldn't work properly.

          By the way, I think you should use QStyledItemDelegate as base class for your delegate since it's the default class used for the item views.

          Thanks for the tip!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved