Results 1 to 20 of 34

Thread: Word wrap in QListView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Word wrap in QListView

    Well, I have some new and some code to post.

    Following marcel's suggestion I have modified the stardelegate example removing the editor and related parts. I have changed the parameter passed to StarRating (from int, the rating, to QString, the text to wordwrap) and used the code from this post; in this way the tableWidget contains a rich text column and long text is wrapped but it overlaps rows below: the cell height is not updated to enclose the multi-line text (hoping i was clear!).

    Then i have made a new copy of the source code and substituted QTableWidget with QListWidget; after some editing i have obtained a working application but without wordwrap (it used a horizontal scrollbar if text is too long).

    My questions are:
    1) how disable scrollbar in qlistwidget and fix the width forcing word wrap?
    2) how change row height according wordwrapping?

    The application, like Star Delegate example, has 2 classes:
    - TextDelegate.
    - TextPainter.

    Here the code (sorry for the long post):

    textdelegate.h
    Qt Code:
    1. #ifndef TEXTDELEGATE_H
    2. #define TEXTDELEGATE_H
    3.  
    4. #include <QItemDelegate>
    5.  
    6. class TextDelegate : public QItemDelegate
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. TextDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
    12.  
    13. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    14. const QModelIndex &index) const;
    15. QSize sizeHint(const QStyleOptionViewItem &option,
    16. const QModelIndex &index) const;
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    textdelegate.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "textdelegate.h"
    4. #include "textpainter.h"
    5.  
    6. void TextDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    7. const QModelIndex &index) const
    8. {
    9. if (qVariantCanConvert<TextPainter>(index.data())) {
    10. TextPainter textPainter = qVariantValue<TextPainter>(index.data());
    11.  
    12. if (option.state & QStyle::State_Selected)
    13. painter->fillRect(option.rect, option.palette.highlight());
    14.  
    15. textPainter.paint(painter, option, TextPainter::ReadOnly);
    16. } else {
    17. QItemDelegate::paint(painter, option, index);
    18. }
    19. }
    20.  
    21. QSize TextDelegate::sizeHint(const QStyleOptionViewItem &option,
    22. const QModelIndex &index) const
    23. {
    24. if (qVariantCanConvert<TextPainter>(index.data())) {
    25. TextPainter textPainter = qVariantValue<TextPainter>(index.data());
    26. return textPainter.sizeHint();
    27. } else {
    28. return QItemDelegate::sizeHint(option, index);
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    textpainter.h
    Qt Code:
    1. #ifndef TEXTPAINTER_H
    2. #define TEXTPAINTER_H
    3.  
    4. #include <QMetaType>
    5. #include <QString>
    6.  
    7. class TextPainter
    8. {
    9. public:
    10. enum EditMode { Editable, ReadOnly };
    11.  
    12. TextPainter(QString text = "");
    13.  
    14. void paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const;
    15. QSize sizeHint() const;
    16.  
    17. private:
    18.  
    19. QString itemText;
    20. };
    21.  
    22. Q_DECLARE_METATYPE(TextPainter)
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    textpainter.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "textpainter.h"
    4.  
    5. const int PaintingScaleFactor = 20;
    6.  
    7. TextPainter::TextPainter(QString text)
    8. {
    9. itemText = text;
    10. }
    11.  
    12. QSize TextPainter::sizeHint() const
    13. {
    14. return PaintingScaleFactor * QSize(itemText.length()/2-2, 1);
    15. }
    16.  
    17. void TextPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const
    18. {
    19. if (mode == Editable) {
    20. painter->setBrush(option.palette.highlight());
    21. } else {
    22. painter->setBrush(option.palette.foreground());
    23. }
    24.  
    25.  
    26. painter->save();
    27. doc.setHtml(itemText);
    28. QAbstractTextDocumentLayout::PaintContext context;
    29. doc.setPageSize( option.rect.size());
    30. painter->translate(option.rect.x(), option.rect.y()-20);
    31. doc.documentLayout()->draw(painter, context);
    32. painter->restore();
    33. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jiveaxe; 3rd August 2007 at 15:46.
    Giuseppe CalÃ

  2. The following user says thank you to jiveaxe for this useful post:

    liuyanghejerry (26th August 2010)

Similar Threads

  1. QListView word wrap
    By serega in forum Qt Programming
    Replies: 17
    Last Post: 30th August 2007, 03:13

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.