PDA

View Full Version : Example code for QTableWidget with images



bruccutler
27th April 2007, 19:15
I want to have images in some of the columns of my table. I have the added icons, but they are too small. How do I scale the images to fill the cell?
-- BRC

marcel
27th April 2007, 19:45
Use QPixmap::scaled. You can choose to keep the aspect ratio.

Regards

bruccutler
27th April 2007, 19:55
Thanks. That worked. Now, how do I center the image in the cell?
- BRC

celine
7th May 2008, 09:27
Hello,

Did you find an answer to your previous question: how to center an image?
It's exactly what I am looking for!

jpn
12th May 2008, 14:07
#include <QtGui>

class TableWidget : public QTableWidget
{
protected:
QStyleOptionViewItem viewOptions() const
{
QStyleOptionViewItem opt = QTableWidget::viewOptions();
opt.decorationPosition = QStyleOptionViewItem::Bottom;
opt.decorationAlignment = Qt::AlignCenter;
return opt;
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
TableWidget t;
t.setRowCount(4);
t.setColumnCount(4);
for (int r = 0; r < t.rowCount(); ++r)
{
for (int c = 0; c < t.columnCount(); ++c)
{
QTableWidgetItem* item = new QTableWidgetItem;
item->setIcon(QIcon(":/trolltech/styles/commonstyle/images/left-128.png"));
t.setItem(r, c, item);
}
}
t.show();
return a.exec();
}