Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34

Thread: Word wrap in QListView

  1. #21
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    In ListWidgetItemDelegate::sizeHint you can pass parent() to SectionItem::sizeHint(). All you have to do is to add another parameter to SectionItem's sizeHint method.
    SectionItem is not a QAbstractItemDelegate, isn't it?

    Regards

  2. #22
    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

    No marcel, SectionItem is not a QAbstractItemDelegate. Now I edit the code and then tell you the result.

    Thanks
    Giuseppe CalÃ

  3. #23
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    Yes. Just make sure you pass the parent of ListWidgetItemDelegate.

    Regards

  4. #24
    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

    I'm making some sort of mistake because i got errors during compiling; here the modifications:

    listwidgetitemdelegate.cpp
    Qt Code:
    1. ...
    2. return sectionItem.sizeHint(parent, option, index);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    sectionitem.h
    Qt Code:
    1. ...
    2. QSize sizeHint(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    3. ...
    To copy to clipboard, switch view to plain text mode 

    sectionitem.cpp
    Qt Code:
    1. QSize SectionItem::sizeHint(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {...}
    To copy to clipboard, switch view to plain text mode 

    For clearness here the code for the header files:
    listwidgetitemdelegate.h
    Qt Code:
    1. class ListWidgetItemDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ListWidgetItemDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
    7.  
    8. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    9.  
    10. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    11. };
    To copy to clipboard, switch view to plain text mode 

    sectionitem.h
    Qt Code:
    1. class SectionItem
    2. {
    3. public:
    4.  
    5. SectionItem(QString text = "");
    6. ~SectionItem();
    7.  
    8. void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
    9. QSize sizeHint(QObject *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    10.  
    11. private:
    12. QString itemText;
    13. };
    14.  
    15. Q_DECLARE_METATYPE(SectionItem)
    To copy to clipboard, switch view to plain text mode 

    Bye
    Giuseppe CalÃ

  5. #25
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    Who is parent? The one that you pass to SectionItem::sizeHint().
    It should be (QListWidget*)parent().

    And in maindialog.cpp, it should be:
    Qt Code:
    1. listWidget->setItemDelegate(new ListWidgetItemDelegate(listWidget));
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to marcel for this useful post:

    jiveaxe (1st September 2007)

  7. #26
    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

    Ok marcel, now works! Here the modifications I made:

    maindialog.cpp
    Qt Code:
    1. listWidget->setItemDelegate(new ListWidgetItemDelegate(listWidget));
    To copy to clipboard, switch view to plain text mode 

    listwidgetitemdelegate.cpp
    Qt Code:
    1. ...
    2. return sectionItem.sizeHint(parent(), option, index);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    sectionitem.h
    Qt Code:
    1. QSize sizeHint(QObject *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    To copy to clipboard, switch view to plain text mode 

    sectionitem.cpp
    Qt Code:
    1. QSize SectionItem::sizeHint(QObject *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3. QListWidget *p = (QListWidget*)parent;
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot
    Giuseppe CalÃ

  8. #27
    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

    I would like rendering html text instead of plain text; changing SectionItem:aint like this:

    Qt Code:
    1. void SectionItem::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
    2. {
    3. painter->save();
    4. doc.setHtml(itemText);
    5. QAbstractTextDocumentLayout::PaintContext context;
    6. doc.setPageSize(option.rect.size());
    7. painter->translate(option.rect.x(), option.rect.y());
    8. doc.documentLayout()->draw(painter, context);
    9. painter->restore();
    10. // painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString());
    11. }
    To copy to clipboard, switch view to plain text mode 

    It renders html text but the last row of each phrase (when wrapped) is always drawn in the following item and a blank row remains in own item (see the attached image for better explanation).

    Any hint?

    Cheers
    Attached Images Attached Images
    Giuseppe CalÃ

  9. #28
    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

    I have found a possible solution even if for some widths it renders not optimally. Attached are 2 images, one with correct draw and one with the unwanted behavior.

    Here the new code for SectionItem:aint

    Qt Code:
    1. void SectionItem::paint(QObject *parent, QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
    2. {
    3. QListWidget *p = (QListWidget*)parent;
    4.  
    5. painter->save();
    6. doc.setHtml(itemText);
    7. QAbstractTextDocumentLayout::PaintContext context;
    8. doc.setPageSize(QSize(option.rect.size().width(), option.rect.size().height() + p->viewport()->size().height()));
    9. painter->translate(option.rect.x(), option.rect.y());
    10. doc.documentLayout()->draw(painter, context);
    11. painter->restore();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Regards
    Attached Images Attached Images
    Giuseppe CalÃ

  10. #29
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    It might have to do with the HTML itself. Do you have any <p> tags or any other tag that enforces spacings around it?

    Can you paste the html?

    BTW: don't tell me that you too are working on (yet another) IDE.

    Regards

  11. #30
    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

    There aren't <p> tags in the html, only <b> and </b>, but here the code:

    Qt Code:
    1. list << "New Section"
    2. << "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class. The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class."
    3. << "The <b>QObject</b> class is the base class of all Qt objects."
    4. << "Qt's <b>drag and drop</b> infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data.";
    To copy to clipboard, switch view to plain text mode 

    ...and not, I am not working on a new ide.

    Bye
    Giuseppe CalÃ

  12. #31
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    I can't say what's wrong.
    Maybe if you can post a minimal example or event the whole code, so I can be able to test it.

    Regards

  13. #32
    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

    Attached there is the code.

    Thanks for your time
    Attached Files Attached Files
    Giuseppe CalÃ

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

    Maxz (12th August 2009)

  15. #33
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Word wrap in QListView

    Qt Code:
    1. void SectionItem::paint(QObject *parent, QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const
    2. {
    3. QListWidget *p = (QListWidget*)parent;
    4.  
    5. painter->save();
    6. doc.setHtml(itemText);
    7. doc.setTextWidth(option.rect.size().width());
    8. QAbstractTextDocumentLayout::PaintContext context;
    9. painter->translate(option.rect.x(), option.rect.y());
    10. doc.documentLayout()->draw(painter, context);
    11. painter->restore();
    12. }
    13.  
    14.  
    15. QSize SectionItem::sizeHint(QObject *parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    16. {
    17. QListWidget *p = (QListWidget*)parent;
    18. QFontMetrics fm(p->font());
    19.  
    20. float rw = float(p->viewport()->size().width());
    21. float tw = fm.width(itemText);
    22. float ratio = tw/rw;
    23. int lines = 0;
    24. if(ratio - int(ratio) < 0.1f)
    25. lines = int(ratio);
    26. else
    27. lines = int(ratio) + 1;
    28. return QSize(rw,lines*fm.height());
    29. }
    To copy to clipboard, switch view to plain text mode 

    Problem 1: don't give the height to the text document. you already set it with the size hint.

    Problem 2: ocassionaly, due to rounding errors, you got 1 extra line of text that remained empty( the +1 in the sizeHint).

    Regards

  16. The following 2 users say thank you to marcel for this useful post:

    jiveaxe (1st September 2007), liuyanghejerry (26th August 2010)

  17. #34
    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

    Thank for your tips, marcel, helpful as usual; I have corrected the code.

    Regards
    Giuseppe CalÃ

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.