Results 1 to 3 of 3

Thread: QStyledItemDelegate - draw an items with a different size text and heights

  1. #1
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QStyledItemDelegate - draw an items with a different size text and heights

    I have a QListView with items containing a text. Each text has different length. So I need to draw an item with different height and use word wrap.

    This is perfect example how this must work.

    http://www.qtcentre.org/wiki/index.p...Expanding_list

    But this replace other longest text on click. I wanted to get rid of that code which is resp for click and update. But it just does not work then.

    So question how to do it right. How to calculate a width/height of the coil and the draw a text there word wrapping ?

    Thanks

  2. #2
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyledItemDelegate - draw an items with a different size text and heights

    Some reason option.rect.width() returns 0 in QSize StatusWidgetDelegate::sizeHint()

    Seems in paint() width is set fine. So I am potentially able to draw it correctly but I cannot set correct size of the item.


    Added after 22 minutes:


    So this is what I came up with and works how I want. But some reason I have to set

    Qt Code:
    1. item->setSizeHint(QSize(this->width(), 10));
    To copy to clipboard, switch view to plain text mode 

    during insert an item to give a width for &options in QSize StatusWidgetDelegate::sizeHint(). Well, this is fine for me, but I don't like any fake information like a height = 10 ( this could be whatever value because its quickly overwritten by sizeHint() logic). Also in this example I need to use resizeEvent() to update the width of the all items. This would be not needed if &option in sizeHint() would return correct rect.

    So what I am doing wrong ?

    QListView

    Qt Code:
    1. void List::insert(Status &status)
    2. {
    3. int row = model()->rowCount();
    4.  
    5. item->setSizeHint(QSize(this->width(), 10));
    6. item->setData(status.message(), StatusWidgetDelegate::MessageRole);
    7.  
    8. model()->insertRow(row,item);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void List::resizeEvent(QResizeEvent *e)
    2. {
    3. for( int i = 0; i < model()->rowCount(); i++)
    4. {
    5. model()->item(i)->setSizeHint(QSize(this->width(),10));
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    QStyledItemDelegate

    Qt Code:
    1. StatusWidgetDelegate::StatusWidgetDelegate(QObject *parent) :
    2. QStyledItemDelegate(parent), m_currentItemHeight(0)
    3. {
    4. }
    5.  
    6. QSize StatusWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    7. {
    8. QString text = index.data(MessageRole).toString();
    9. QRectF r = option.rect;
    10. m_currentItemHeight = fm.boundingRect(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text).height();
    11.  
    12. QSize size = QStyledItemDelegate::sizeHint(option, index);
    13. size.setHeight(m_currentItemHeight);
    14. size.setWidth(size.width() - 10);
    15.  
    16. return size;
    17. }
    18.  
    19. void StatusWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    20. {
    21. QString text = index.data(MessageRole).toString();
    22. QRectF r = option.rect;
    23. painter->save();
    24. painter->setPen(Qt::NoPen);
    25. painter->setBrush(QColor(220,220,250));
    26. painter->drawRect(r);
    27. painter->restore();
    28. painter->drawText(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for looking...


    Added after 47 minutes:


    The other thing is that a QListView scroll bar does not react on new delegates. Moving Bottom Window up to resize a height of the entire mainWindow does not show a QListView scroll bar. so long list items is behind and not visible.

    Something is wrong here.
    Last edited by migel; 27th December 2011 at 23:28.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QStyledItemDelegate - draw an items with a different size text and heights

    pff! That was an old wiki article I wrote without ever polish it...

    If I understand you right, you just want a list with different row heights to fit the content. It is as simple as this
    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class Delegate : public QItemDelegate
    5. {
    6. public:
    7. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
    8. return option.fontMetrics.boundingRect(option.rect, Qt::TextWordWrap, index.data().toString()).size();
    9. }
    10. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    11. if (option.state & QStyle::State_Selected) {
    12. painter->fillRect(option.rect, option.palette.highlight());
    13. painter->setPen(option.palette.highlightedText().color());
    14. } else {
    15. painter->setPen(option.palette.text().color());
    16. }
    17. painter->drawText(option.rect, Qt::TextWordWrap, index.data().toString());
    18. }
    19. };
    20.  
    21. int main(int argC, char **argV)
    22. {
    23. QApplication myApp(argC, argV);
    24. m.setStringList(QStringList()
    25. << "test"
    26. << "long test long test long test long test"
    27. << "even longer test even longer test even longer test even longer test even "
    28. "longer test even longer test even longer test even longer test even longer "
    29. "test even longer test even longer test even longer test even longer test "
    30. "even longer test");
    31.  
    32. Delegate d;
    33. v.setItemDelegate(&d);
    34. v.setModel(&m);
    35. v.show();
    36.  
    37. return myApp.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

    Best

Similar Threads

  1. Replies: 0
    Last Post: 20th April 2011, 13:42
  2. Replies: 10
    Last Post: 10th February 2011, 23:31
  3. Replies: 1
    Last Post: 9th September 2010, 18:26
  4. Replies: 6
    Last Post: 16th February 2010, 18:21
  5. Replies: 5
    Last Post: 30th March 2008, 16:53

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.