Results 1 to 3 of 3

Thread: Custom Delegate and Text Wrap

  1. #1
    Join Date
    Aug 2010
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Custom Delegate and Text Wrap

    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 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Custom Delegate and Text Wrap

    The QPainter::boundingRect() return value should tell you the actual size of the rectangle that the text will occupy. So the r.height() value should give you an idea where to put the top edge of the image.

  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: Custom Delegate and Text Wrap

    ...or you can use QFontMetrics::boundingRect(). Get it via option.fontMetrics.

Similar Threads

  1. How to wrap text in a label?
    By sasi in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2016, 05:14
  2. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 21:23
  3. Long text and word wrap in QPainter
    By TomASS in forum Qt Programming
    Replies: 2
    Last Post: 11th December 2009, 12:50
  4. Wrap Text in PUSHBUTTON
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 14:54
  5. how can I make a balloon to wrap the editable text?
    By learning_qt in forum Qt Programming
    Replies: 9
    Last Post: 20th January 2009, 16:09

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.