Results 1 to 5 of 5

Thread: performance issue(QGV + QPixmap::grabWidget)

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default performance issue(QGV + QPixmap::grabWidget)

    Hi,
    I have a QTabWidget derived class and I have implemented some sort of a ThumbnailMode, where every Tab is displayed as a small Thumbnail in a QGraphicsView and upon clicking on the Thumbnail the corresponding Tab is activated.

    The Problem is that creating and displaying the Thumbnails seems to be pretty cpuintensive. With about 20 Tabs it takes approximately one second to switch to Thumbnailmode. Is there a way to improve the performance somehow?
    Below, I have pasted the corresponding functions and the QGraphicsItem derived class. If someone needs a minimal compilable version let me know.

    Thanx in advance


    p.s. Qt-4.4 will hopefully support QWidgets on QGV ...

    The functions that populate the scene with items
    Qt Code:
    1. void ThumbnailScene::createItems()
    2. {
    3. foreach(QGraphicsItem *item, items) {
    4. delete(item);
    5. }
    6. invalidate(sceneRect());
    7. items.clear();
    8.  
    9.  
    10. for (int i=0; i<tabWidget->count(); i++) {
    11. QWidget *w = tabWidget->widget(i);
    12. QPixmap pm = QPixmap::grabWidget(w);
    13.  
    14. ThumbnailItem * item = new ThumbnailItem(pm, i); // Take size from config
    15. items << item;
    16. item->scale(0.2, 0.2);
    17. connect(item, SIGNAL(tabSelected(int)), tabWidget, SLOT(onTabSelected(int)));
    18. connect(item, SIGNAL(tabSelected(int)), this, SIGNAL(toggleThumbnailMode()));
    19. addItem(item);
    20. }
    21. }
    22.  
    23.  
    24. void ThumbnailScene::layoutThumbs(int w) // w is the width of the scene
    25. {
    26. int padding, x, y, maxw;
    27. padding = 20;
    28. x = 0;
    29. y = padding;
    30. maxw = w;
    31.  
    32. foreach(QGraphicsItem * item, items) {
    33. QRectF bRect = item->boundingRect();
    34. QPointF tl,br;
    35. tl = item->mapToScene(bRect.topLeft());
    36. br = item->mapToScene(bRect.bottomRight());
    37. QRectF rect(tl, br);
    38.  
    39. if (x + rect.width() > maxw) {
    40. x = 0;
    41. y += rect.height() + padding;
    42. }
    43.  
    44. item->setPos(x, y);
    45. x += rect.width() + padding;
    46. }
    47.  
    48. setSceneRect(0, 0, itemsBoundingRect().width(), itemsBoundingRect().height() + padding*2);
    49. }
    To copy to clipboard, switch view to plain text mode 
    The implementation of the QGraphicsItem
    Qt Code:
    1. ThumbnailItem::ThumbnailItem(const QPixmap& pixmap, int index, QGraphicsItem *parent)
    2. : QGraphicsItem(parent), mIndex(index), pm(pixmap)
    3. {
    4. setAcceptsHoverEvents(true);
    5. }
    6.  
    7. QRectF ThumbnailItem::boundingRect() const
    8. {
    9. return QRectF(0, 0, pm.width(), pm.height());
    10. }
    11.  
    12. void ThumbnailItem::paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
    13. {
    14. p->setRenderHints(QPainter::Antialiasing);
    15. p->setPen(QPen(Qt::gray, 6));
    16. p->drawPixmap(0, 0, pm);
    17. p->drawRect(0, 0, pm.width(), pm.height());
    18. }
    To copy to clipboard, switch view to plain text mode 

  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: performance issue(QGV + QPixmap::grabWidget)

    First of all don't use item->scale, because this will make the pixmap scale each time the item is redrawn. Scale the pixmap itself, so that each is scaled only once. And second of all you can add items in batches - like add 4 items, process events, add another 4 items, process events, etc. This way you won't block the application and you'll be able to perform other tasks while thumbnails are being built.

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

    momesana (27th September 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: performance issue(QGV + QPixmap::grabWidget)

    Thanks for the reply wysota. According to your suggestions, I have changed the function createItems in the following way:
    Qt Code:
    1. for (int i=0; i<tabWidget->count(); i++) {
    2. foreach(QGraphicsItem *item, items) {
    3. delete(item);
    4. }
    5. invalidate(sceneRect());
    6. items.clear();
    7.  
    8. int batchSz = 4;
    9. for (int i=0; i<tabWidget->count(); i++) {
    10. QWidget *w = tabWidget->widget(i);
    11. QPixmap pm = QPixmap::grabWidget(w);
    12. pm = pm.scaled(300,200, Qt::IgnoreAspectRatio);
    13. ThumbnailItem * item = new ThumbnailItem(pm, i); // TODO Take size from config
    14. items << item;
    15. connect(item, SIGNAL(tabSelected(int)), tabWidget, SLOT(onTabSelected(int)));
    16. connect(item, SIGNAL(tabSelected(int)), this, SIGNAL(toggleThumbnailMode()));
    17. if (items.count() % batchSz == 0) {
    18. foreach(QGraphicsItem *i, items.mid(items.count()-4))
    19. addItem(i);
    20. QCoreApplication::processEvents();
    21. }
    22. }
    23. foreach(QGraphicsItem *i, items.mid(items.count() - items.count()%batchSz))
    24. addItem(i);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Now the switch to thumbnail-mode is immediate which is a good thing. But, now the users see the items being created which is not optimal. Is it expensive to add some QGV Animation to hide this from the user? Maybe to display an opaque QGraphicsRectItem that covers the whole scene and then fading it out using QTimeLine in conjunction with QGraphicsItemAnimation?

    Thanx
    Last edited by momesana; 27th September 2007 at 12:31.

  5. #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: performance issue(QGV + QPixmap::grabWidget)

    Yes, you can do that. You can even hide() all items after you create them and then when all items have been created, show() all of them in one go.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: performance issue(QGV + QPixmap::grabWidget)

    Thank you. I'll try that .

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.