Unsolved QListview items are clipped on the left side
-
I am trying to draw qwidgets in a listview with a delegate. I can get the widget to render but it seems like there is clip window that is moving over the widget in the left direction as I draw them and I can't figure why. It's almost like the listview is adding a margin that grows with each item that is added, but somehow I am still drawing the widget at the right location.
Kind of like this.
<clip> -------------------- <clip-clip> --------------------- void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { MyWidget w("test", "test"); //At some point I want to load the widget with data from here if(index.data().canConvert<CustomModelItem>()) { w.resize(option.rect.size()); painter.save(); //some kind of bug with passing topLeft in. I found this solution somewhere w.render(painter, painter->deviceTransform().map(option.rect.topLeft())); painter.restore(); } else { QStyledItemDelegate::paint(painter, option, index); } }
Can anyone explain this behavior?
-
Hi,
Do you have that effect with the current code ? I must say I don't see the need of calling deviceTransform.
Did you take a look at the Star Delegate example ? It might help you get started with custom painting.
-
Star delegate manually draws a star. That is not what I want. I want to draw a widget specifically to pick up the style properties from a stylesheet. I can draw lines anywhere I want all day. It's the rendering of a widget that I am struggling with. I did a little more investigation and what I found is that the system clip QRegion in the QPainter is moving +3px to the right on every iteration in the loop of QListview's paint method. That completely explains the behavior that I am seeing, but I can't figure out why it is happening. I have been trying to see how QListWidget accomplished its drawing. It is definitely not calling render on the items in the list. It appears as though it is calling drawControl(...). I can't say for certain but I think QListWidget is simply drawing sections of itself. Each section is defined by the rectangle that represents one list item.
I see one call to render for the what I assume is the viewport.
Then I see a QStyle::drawControl() for every item in the list. However, the option.widget is always the viewport The only thing different is the target offset and the clip region. This is what makes me think it simply drawing the same widget in "chunks". I tried drawing my widget with QStyle::drawControl(), but I can't get it to render.
-
Can you share a minimal code sample that reproduces this ?