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