Results 1 to 18 of 18

Thread: QListView word wrap

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #18
    Join Date
    May 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QListView word wrap

    I haven't played with my application for a while, now I came back to it and to sizeHint(). Below is my current implementation, which provides may be not perfect, but very good results.
    There was some questions above regarding parent(). Ideally QAbstractItemDelegate do not need to know about QAbstractItemView, but in my case I have to set the view as a parent of the delegate because I need the width of the view to determine the height.
    Qt Code:
    1. QSize PartOfSpeechItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QAbstractItemView *view = dynamic_cast<QAbstractItemView*>(parent());
    4. QFontMetricsF fontMetrics(view->fontMetrics());
    5. QString str = index.data().toString().trimmed();
    6. QStringList words = str.split(QChar(' '));
    7. QRectF boundingRect = fontMetrics.boundingRect(str);
    8. int width = view->viewport()->width() - 6;
    9. int times = 0;
    10. while (words.size() > 0) {
    11. times++;
    12. qreal lineWidth = 0;
    13. bool enoughSpace = true;
    14. do {
    15. QString word = words.first();
    16. qreal wordWidth = fontMetrics.width(word);
    17. if (wordWidth + lineWidth < width) {
    18. lineWidth += wordWidth;
    19. lineWidth += fontMetrics.width(QChar(' '));
    20. words.removeFirst();
    21. } else
    22. enoughSpace = false;
    23.  
    24. } while (enoughSpace && words.size() > 0);
    25. }
    26. return QSize(width, boundingRect.height() * times - times + 1);
    27. }
    28.  
    29. void PartOfSpeechItemDelegate::paint(QPainter *painter,
    30. const QStyleOptionViewItem &option, const QModelIndex &index) const
    31. {
    32. QRect rect(option.rect.topLeft(), option.rect.bottomRight());
    33. if (option.state & QStyle::State_Selected)
    34. painter->fillRect(rect, option.palette.highlight());
    35.  
    36. painter->save();
    37. painter->setPen(QPen(Qt::gray));
    38. painter->drawLine(rect.topLeft(), rect.topRight());
    39. painter->restore();
    40.  
    41. rect.setLeft(rect.left() + 4);
    42. rect.setTop(rect.top() + 2);
    43. rect.setRight(rect.right() - 2);
    44. painter->drawText(rect, Qt::TextWordWrap , index.data().toString());
    45. }
    To copy to clipboard, switch view to plain text mode 

    My view:
    Qt Code:
    1. void PartOfSpeechItemView::resizeEvent(QResizeEvent *resizeEvent)
    2. {
    3. reset();
    4. QListView::resizeEvent(resizeEvent);
    5. }
    To copy to clipboard, switch view to plain text mode 

    See the attachment how it looks like.

    serega.
    Attached Images Attached Images

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.