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->setColumnCount(2);
t->setHorizontalHeaderLabels(labels);
t->setItemDelegateForColumn(1, new ImageDelegate(this));
for(int i=0;i < 100; i++)
{
t->insertRow(i);
item[i][0]->setFlags(item[i][0]->flags() & ~Qt::ItemIsEditable);
t->setItem(i, 0, item[i][0]);
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();
}
...
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();
}
To copy to clipboard, switch view to plain text mode
imagedelegate.cpp:
#include <QtGui>
#include "imagedelegate.h"
ImageDelegate
::ImageDelegate(QObject *parent
) {
}
{
QPixmap pixmap
(index.
model()->data
(index
).
toString());
painter->save();
painter->drawPixmap(option.rect, pixmap);
painter->restore();
}
{
}
#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));
}
To copy to clipboard, switch view to plain text mode
Bookmarks