Hi,

I have a custom delegate that draws text and an image right beneath the text for each item in a QStandardItemModel like the attached screenshot. Screenshot.png

As you can see, the text is covered by the image once it wraps because image location is fixed for each item. I could not figure out a way to query for the Y position of the text that will be painted after calling drawText() and set my image location accordingly. Google search and browse through this forum does not reveal any hint, please suggest how I could accomplish it.

I have attached a simplified version of code to reproduce the behavior and thanks in advance for any reply!!
textwrap.tar.bz2
The current paint code looks like:
Qt Code:
  1. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  2. {
  3. QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter );
  4. painter->save();
  5. painter->translate(option.rect.topLeft());
  6.  
  7. const QRect line(0, 0, option.rect.width(), option.rect.height());
  8. painter->setClipRect(line);
  9.  
  10. if(option.state & QStyle::State_Selected) {
  11. painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
  12. }
  13.  
  14. QString txt = index.data(Qt::DisplayRole).toString();
  15. QRect r = QRect(PADDING, 0, option.rect.width() - PADDING, option.rect.height() - PADDING);
  16. r = painter->boundingRect(r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, txt);
  17. painter->drawText(r, Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, txt);
  18.  
  19. QImage ic = QImage(qvariant_cast<QImage>(index.data(Qt::DecorationRole)));
  20. r = QRect(0, PADDING*2, PIC_WIDTH, PIC_HEIGHT);
  21. painter->drawImage(r, ic);
  22. painter->restore();
  23. }
To copy to clipboard, switch view to plain text mode