Results 1 to 12 of 12

Thread: Delegate paint optimisation

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Delegate paint optimisation

    Here is two delegated QListView of 1000 items,
    One is fast, the other is slow.



    When the area is wider, the painting is *much* slower.

    Here is my delegate paint function
    Qt Code:
    1. void ZeChatRoom_delegate::paint(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. // QItemDelegate::paint(painter, option, index);
    6.  
    7. ZeChatRoom_data * chatData = index.data().value<ZeChatRoom_data *>();
    8.  
    9. // Display rects
    10. QRect pictureRect(option.rect.x() + 2, option.rect.y() + 2, 32, 32);
    11.  
    12. QRect displayRect = QRect(option.rect.x() + 36, option.rect.y() + 2,
    13. option.rect.width() - 38, (option.rect.height() - 4) / 2);
    14.  
    15. QRect displayRect2 = QRect(option.rect.x() + 36, option.rect.y() + option.rect.height() / 2,
    16. option.rect.width() - 38, (option.rect.height() - 4) / 2);
    17.  
    18. QColor textColor = Qt::black;
    19. QColor descriptionColor = ZeStyle::get()->getDarkBorderColor();
    20.  
    21. if (option.state & QStyle::State_MouseOver)
    22. {
    23. ZePainterController::get()->DrawHighlight(*painter, option.rect);
    24.  
    25. textColor = ZeStyle::get()->getHighlightTextColor();
    26. descriptionColor = ZeStyle::get()->getHighlightTextColor();
    27.  
    28. ZePainterController::get()->
    29. DrawBottomLine(*painter, option.rect, QColor(100, 100, 100, 150));
    30. }
    31. else
    32. {
    33. if (index.row() %2 == 0)
    34. {
    35. ZePainterController::get()->
    36. DrawBack(*painter, option.rect, QColor(ZeStyle::get()->getBorderColor().red(),
    37. ZeStyle::get()->getBorderColor().green(),
    38. ZeStyle::get()->getBorderColor().blue(), 50), false);
    39. }
    40.  
    41. ZePainterController::get()->
    42. DrawBottomLine(*painter, option.rect,
    43. /*QColor(100, 100, 100, 150)*/ QColor(ZeStyle::get()->getBorderColor().red(),
    44. ZeStyle::get()->getBorderColor().green(),
    45. ZeStyle::get()->getBorderColor().blue(), 200));
    46. }
    47.  
    48. ZePainterController::get()->
    49. DrawPictureRatio(*painter, *chatData->mChatPixmap, pictureRect, 1.0f);
    50.  
    51. QFont font;
    52. font.setBold(true);
    53. ZePainterController::get()->DrawText(*painter,
    54. displayRect,
    55. chatData->mRoomName + " - " + QString::number(chatData->mParticipants),
    56. Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine,
    57. textColor,
    58. &font);
    59.  
    60. ZePainterController::get()->DrawText(*painter,
    61. displayRect2,
    62. chatData->mDescription,
    63. Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine,
    64. descriptionColor);
    65. }
    To copy to clipboard, switch view to plain text mode 

    Is there a way to optimize the painting ? Especially when you're painting plain grey rectangles at each refresh.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    Hard to say without knowing what all those functions of yours do.

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Delegate paint optimisation

    This one draws the grey background:

    Qt Code:
    1. void ZePainterController::DrawBack(QPainter & painter, const QRect &rect,
    2. const QColor & color, const bool border)
    3. {
    4. QRect rectBase;
    5.  
    6. if (border)
    7. {
    8. QPen pen;
    9. pen.setBrush(ZeStyle::get()->getBorderColor());
    10. painter.setPen(pen);
    11.  
    12. rectBase.setRect(rect.x(), rect.y(),
    13. rect.width() - 1, rect.height() - 1);
    14. }
    15. else
    16. {
    17. painter.setPen(Qt::NoPen);
    18.  
    19. rectBase.setRect(rect.x(), rect.y(),
    20. rect.width(), rect.height());
    21. }
    22.  
    23. painter.setBrush(QColor(color.red(), color.green(), color.blue(), color.alpha()));
    24. painter.drawRect(rectBase);
    25. }
    To copy to clipboard, switch view to plain text mode 

    This one draws the text:

    Qt Code:
    1. void ZePainterController::DrawText(QPainter & painter,
    2. const QRectF & rect,
    3. const QString & text,
    4. int flags,
    5. const QColor & color,
    6. const QFont * font,
    7. const bool elided)
    8. {
    9. painter.save();
    10.  
    11. QString finalText;
    12.  
    13. if (font)
    14. {
    15. painter.setFont(*font);
    16. }
    17.  
    18. if (elided)
    19. {
    20. QFontMetrics fontMetric(painter.font());
    21. finalText = fontMetric.elidedText(text,
    22. Qt::ElideRight,
    23. rect.width());
    24. }
    25. else
    26. {
    27. finalText = text;
    28. }
    29.  
    30. QPen pen;
    31.  
    32. pen.setBrush(color);
    33. painter.setPen(pen);
    34.  
    35. painter.drawText(rect, flags, finalText);
    36.  
    37. painter.restore();
    38. }
    To copy to clipboard, switch view to plain text mode 

    This one draws the picture

    Qt Code:
    1. void ZePainterController::DrawPictureRatio(QPainter & painter, QPixmap & pixmap,
    2. const QRect &rect, const float opacity)
    3. {
    4. int gapWidth = 0;
    5. int gapHeight = 0;
    6.  
    7. if (rect.width() > pixmap.width())
    8. {
    9. gapWidth = (rect.width() - pixmap.width()) / 2;
    10. }
    11. if (rect.height() > pixmap.height())
    12. {
    13. gapHeight = (rect.height() - pixmap.height()) / 2;
    14. }
    15.  
    16. QRect displayRect(rect.x() + gapWidth, rect.y() + gapHeight,
    17. pixmap.width(), pixmap.height());
    18.  
    19. DrawPicture(painter, pixmap, displayRect, opacity);
    20. }
    To copy to clipboard, switch view to plain text mode 

    ...Hope that didn't kill you .

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    I don't see any reason why increasing the width should significantly reduce rendering speed. Have you tried using a profiler to find the bottleneck?

  5. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Delegate paint optimisation

    Maybe it's my QT 4.4 Release candidate. I'll do some more tests :/

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    No, there is really nothing that would cause such a slowdown there, regardless of the Qt version used. I really suggest profiling.

  7. The following user says thank you to wysota for this useful post:

    bunjee (21st April 2008)

  8. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Delegate paint optimisation

    Alright,
    Transparency seems to be the problem.

    I have several QListViews in a list of widget.

    The listviews have their back set to transparent :
    Qt Code:
    1. QPalette palette;
    2. palette.setColor(QPalette::Base, QColor(0, 0, 0, 0));
    3. mListView->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    The widget list has a custom paint event, painting a custom color.

    Several layers of transparency seems to reduce performances.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    Where exactly do you have those layers of transparency?

  10. The following user says thank you to wysota for this useful post:

    bunjee (21st April 2008)

  11. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Delegate paint optimisation

    I had the following paint event in my *Home made* list of widgets:

    Qt Code:
    1. void ZeBaseListWidget::paintEvent(QPaintEvent * event)
    2. {
    3. mScrollArea->setBackgroundColor(mListColor);
    4. QPainter painter(this);
    5.  
    6. painter.setPen(Qt::NoPen);
    7. if (mListColor == Qt::white)
    8. {
    9. painter.setBrush(ZeStyle::get()->getColorList());
    10. }
    11. else
    12. {
    13. painter.setBrush(mListColor);
    14. }
    15.  
    16. painter.drawRect(rect());
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now I'm doing like that:

    Qt Code:
    1. QPalette palette;
    2. palette.setColor(QPalette::Background, color);
    3. setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    If specify the background palette instead of painting it, performances are better.

    On the flipside, I have to notify my widget list everytime the background color changes.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    What is mScrollArea? By the way, there are some flags you might use to optimize performance. Caching some things might also speed up things. If you still didn't run your code through a profiler, I really suggest to do that.

  13. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Delegate paint optimisation

    mScrollArea was commented.

    Yes, I'll do some profiling. I need to work on memory consumption too, since my program is quite heavy.

    Do you have a particular tool to suggest ?

    Beside this, model view approach works great.


    Off topic, but I like your retargetting implementation (QRetargetScale) on your blog.

    Thanks for all the help you provided me,
    I'll put your name and blog in the "about" of my program .
    Last edited by bunjee; 21st April 2008 at 19:00.

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate paint optimisation

    Quote Originally Posted by bunjee View Post
    Do you have a particular tool to suggest ?
    Depends what OS you are using. I'd suggest gprof or valgrind (cachegrind, to be exact) if they are available for your platform.

    Off topic, but I like your retargetting implementation (QRetargetScale) on your blog.

    Thanks for all the help you provided me,
    I'll put your name and blog in the "about" of my program .
    Thx

Similar Threads

  1. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

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.