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
void ThumbnailScene::createItems()
{
delete(item);
}
invalidate(sceneRect());
items.clear();
for (int i=0; i<tabWidget->count(); i++) {
ThumbnailItem * item = new ThumbnailItem(pm, i); // Take size from config
items << item;
item->scale(0.2, 0.2);
connect(item, SIGNAL(tabSelected(int)), tabWidget, SLOT(onTabSelected(int)));
connect(item, SIGNAL(tabSelected(int)), this, SIGNAL(toggleThumbnailMode()));
addItem(item);
}
}
void ThumbnailScene::layoutThumbs(int w) // w is the width of the scene
{
int padding, x, y, maxw;
padding = 20;
x = 0;
y = padding;
maxw = w;
QRectF bRect
= item
->boundingRect
();
tl = item->mapToScene(bRect.topLeft());
br = item->mapToScene(bRect.bottomRight());
if (x + rect.width() > maxw) {
x = 0;
y += rect.height() + padding;
}
item->setPos(x, y);
x += rect.width() + padding;
}
setSceneRect(0, 0, itemsBoundingRect().width(), itemsBoundingRect().height() + padding*2);
}
void ThumbnailScene::createItems()
{
foreach(QGraphicsItem *item, items) {
delete(item);
}
invalidate(sceneRect());
items.clear();
for (int i=0; i<tabWidget->count(); i++) {
QWidget *w = tabWidget->widget(i);
QPixmap pm = QPixmap::grabWidget(w);
ThumbnailItem * item = new ThumbnailItem(pm, i); // Take size from config
items << item;
item->scale(0.2, 0.2);
connect(item, SIGNAL(tabSelected(int)), tabWidget, SLOT(onTabSelected(int)));
connect(item, SIGNAL(tabSelected(int)), this, SIGNAL(toggleThumbnailMode()));
addItem(item);
}
}
void ThumbnailScene::layoutThumbs(int w) // w is the width of the scene
{
int padding, x, y, maxw;
padding = 20;
x = 0;
y = padding;
maxw = w;
foreach(QGraphicsItem * item, items) {
QRectF bRect = item->boundingRect();
QPointF tl,br;
tl = item->mapToScene(bRect.topLeft());
br = item->mapToScene(bRect.bottomRight());
QRectF rect(tl, br);
if (x + rect.width() > maxw) {
x = 0;
y += rect.height() + padding;
}
item->setPos(x, y);
x += rect.width() + padding;
}
setSceneRect(0, 0, itemsBoundingRect().width(), itemsBoundingRect().height() + padding*2);
}
To copy to clipboard, switch view to plain text mode
The implementation of the QGraphicsItem
ThumbnailItem
::ThumbnailItem(const QPixmap
& pixmap,
int index,
QGraphicsItem *parent
){
setAcceptsHoverEvents(true);
}
QRectF ThumbnailItem
::boundingRect() const {
return QRectF(0,
0, pm.
width(), pm.
height());
}
{
p
->setRenderHints
(QPainter::Antialiasing);
p
->setPen
(QPen(Qt
::gray,
6));
p->drawPixmap(0, 0, pm);
p->drawRect(0, 0, pm.width(), pm.height());
}
ThumbnailItem::ThumbnailItem(const QPixmap& pixmap, int index, QGraphicsItem *parent)
: QGraphicsItem(parent), mIndex(index), pm(pixmap)
{
setAcceptsHoverEvents(true);
}
QRectF ThumbnailItem::boundingRect() const
{
return QRectF(0, 0, pm.width(), pm.height());
}
void ThumbnailItem::paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget*)
{
p->setRenderHints(QPainter::Antialiasing);
p->setPen(QPen(Qt::gray, 6));
p->drawPixmap(0, 0, pm);
p->drawRect(0, 0, pm.width(), pm.height());
}
To copy to clipboard, switch view to plain text mode
Bookmarks