You are not logged in.

#1 2011-09-10 23:33:37

tony5429
Member
Registered: 2006-03-28
Posts: 1,020

[SOLVED] Resizing Images in QTableWidgetItems

I'm working on a Qt project and trying to create a table which has a column of images. So I created QTableWidgetItems for each of the cells in the column and used setIcon() to apply the images to the cells as QPixMaps. To resize the images I'm trying QPixMap::scaled, however it doesn't seem to be affecting the size of the images which are altogether too tiny... Does anyone have any ideas? I've included a screenshot below as well as a code snippet.

2i0yu02.jpg

QTableWidgetItem *mediaCell = new QTableWidgetItem();
mediaCell->setIcon(QIcon(QPixmap("mypic.jpg").scaled(500, 500, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
mediaTable->setItem(ctr, currCol++, mediaCell);

Last edited by tony5429 (2011-09-11 12:56:00)

Offline

#2 2011-09-11 08:53:06

lunar
Member
Registered: 2010-10-04
Posts: 95

Re: [SOLVED] Resizing Images in QTableWidgetItems

@tony5429:  Please read the documentation of QIcon:

The simplest use of QIcon is to create one from a QPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles and sizes.

In short, it doesn't matter whether you create an icon from the original or from a scaled pixmap, the size is simply ignored.  Qt always chooses the "best" size for an icon, and the best size for an icon in a table row is obviously the height of this row.

If you want to show "images", not icons, try "mediaCell->setData(Qt.ItemDecorationRole, QPixmap("mypic.jpg"))".  Even better, abandon "QTableWidget", and write a proper "QTableModel" for use with "QTableView".

Offline

#3 2011-09-11 12:55:40

tony5429
Member
Registered: 2006-03-28
Posts: 1,020

Re: [SOLVED] Resizing Images in QTableWidgetItems

Thank you so much! This worked for me...

QTableWidgetItem *mediaCell = new QTableWidgetItem();
mediaCell->setData(Qt::DecorationRole, QPixmap("mypic.jpg").scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
mediaTable->setItem(ctr, currCol++, mediaCell);

Offline

Board footer

Powered by FluxBB