PDA

View Full Version : tablewidget and pixmap



magoo
7th November 2006, 20:04
Hi, using Qt 4.2.1, I'd like to display images on one of the fourth column of my tablewidget. Anyone could give me hints on how I can do this ? it's a display-only table.

thanks

wysota
7th November 2006, 21:07
Set the icon of the item to the pixmap you wish to display (using QTableWidgetItem::setIcon().

magoo
8th November 2006, 13:46
yes, but i'd like to display the pixmap at a fixed size and not a tiny icon... how can I do this ?

many thanks

wysota
8th November 2006, 14:19
Provide a new item delegate which will render your pixmap and return a proper size for the item so that the pixmap can fit it. See QAbstractItemDelegate for more info.

magoo
8th November 2006, 18:34
that's cool and I was able to make it works. Another problem I have, why do I have to call resizeColumnsToContents() and resizeRowsToContents() if I add more than one rows to my TableWidget ? The cells size seems to reset after I add new TableWidgetItem to the table... There's probably a way for the table to remember the size of the cells that are already in the table...

Also, calling scrollToBottom() doesn't actually scroll anything...

as an example :

mainwindow.cpp :


...

t = new QTableWidget();
t->setSelectionMode(QAbstractItemView::NoSelection);
t->setColumnCount(2);
t->setHorizontalHeaderLabels(labels);
t->setItemDelegateForColumn(1, new ImageDelegate(this));

QTableWidgetItem *item[100][2];

for(int i=0;i < 100; i++)
{
t->insertRow(i);

item[i][0] = new QTableWidgetItem(s.arg(i));
item[i][0]->setFlags(item[i][0]->flags() & ~Qt::ItemIsEditable);
t->setItem(i, 0, item[i][0]);

item[i][1] = new QTableWidgetItem(":/icons/unknown.png");
item[i][1]->setData(Qt::UserRole, ":/icons/unknown.png");
item[i][1]->setFlags(item[i][1]->flags() & ~Qt::ItemIsEditable);
t->setItem(i, 1, item[i][1]);

//i tried a bunch of stuff... resizeColumnToContents() and resizeRowToContents() works,
//but only without any parameters and I guess it's not very good to recalculate everything
//everytime I add something ? I'd like to just resize the inserted row AND that the already
//inserted row keep their size.

//t->resizeColumnToContents(0);
//t->resizeColumnToContents(1);
//t->resizeRowToContents(i);
//t->resizeColumnsToContents();
//t->resizeRowsToContents();
//t->setColumnWidth(1,140);
//t->setRowHeight(i,200);

//t->scrollToBottom();
}


imagedelegate.cpp:


#include <QtGui>

#include "imagedelegate.h"

ImageDelegate::ImageDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
}

void ImageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QPixmap pixmap(index.model()->data(index).toString());
painter->save();
painter->drawPixmap(option.rect, pixmap);
painter->restore();
}

QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & /* option */,
const QModelIndex & /* index */) const
{
return(QSize(140,200));
}

wysota
8th November 2006, 19:22
Another problem I have, why do I have to call resizeColumnsToContents() and resizeRowsToContents() if I add more than one rows to my TableWidget ? The cells size seems to reset after I add new TableWidgetItem to the table... There's probably a way for the table to remember the size of the cells that are already in the table...

Provide a correct size hint in the delegate. For example:


QSize myDelegate::sizeHint(const QStyleOptionViewItem & /* option */,
const QModelIndex & index) const {
return index.data(Qt::DecorationRole).toPixmap().size()+Q Size(4,4);
}

magoo
8th November 2006, 20:15
I've put a qDebug() inside the sizeHint() method of the delegate and it's never actually called.

I've try adding
item[i][1]->setData(Qt::SizeHintRole, QSize(140,200)); in my mainwindow.cpp but the cells are still sizing incorrectly. if I call the sizeHint() method of the TableWidgetItem, it returns the correct value but the delegate sizeHint() is never call with or without adding the SizeHintRole data.

wysota
8th November 2006, 21:04
Try subclassing QItemDelegate instead of the abstract delegate.