PDA

View Full Version : QTreeWidget - setPixmap???



durbrak
8th October 2006, 20:56
Hello,
in Qt3 I always used QListView and QListViewItem::setPixmap() to put picture in cells instead of text. Now in Qt4 the replacement for QListView seems to be QTreeWidget with its QTreeWidgetItems... but QTreeWidgetItems doesn't have something like setPixmap()... only setIcon() and setData(0, Qt::DecorationRole, pixmap) but those behave like i'm setting some icon instead of a picture.
So with setIcon() my picture is being resized to 16x16 (I CANNOT use QTreeWidget::setIconSize() since my pixmaps have all different dimensions)... with setData() (as DecorationRole) my pixmap is being cut off at left and down...
What's Qt4's way of handling this?

Thank you for your help in advance!

jpn
8th October 2006, 21:09
Maybe a QListWidget in QListView::IconMode suits you better?

durbrak
8th October 2006, 21:15
Unfortunately the QListWidget isn't the type of view I need. My View has got to have columns and a Header and QListWidget is just a simple list box with widgets in it :)

jpn
9th October 2006, 06:44
I have a feeling that QItemDelegate might not be flexible enough for this. However, maybe you could make use of item widgets (http://doc.trolltech.com/4.2/qtreewidget.html#setItemWidget)?

durbrak
9th October 2006, 13:47
I already tried that by setting a QPixmap on a QLabel and putting that QLabel in the QTreeWidget via setItemWidget() but the widget is being cut off (row is not high enough).
I'm having a feeling that TT forgot to integrate this 'simple' feature :(

jpn
9th October 2006, 14:00
Try setting uniform row heights off and appropriate size hints for every item containing an item widget:


treeWidget->setUniformRowHeight(false);
// ...
// for each item:
treeWidgetItem->setSizeHint(col, widget->sizeHint()); // same size hint for the item than the item widget has

durbrak
9th October 2006, 20:37
I'm about to freak out...
I now use my own QTreeView and my own QStandardItemModel but this whole Qt:: DecorationRole instead of the good old Qt3 setPixmap()-method is just pure BS :mad: :mad: :mad:
I've been sitting on a solution for this for 3 days constantly now and I think I'm about to have a heart attack... this cr*p is driving me crazy... my QItemDelegate::drawDecoration()'s QRect supplies some screwed up positions, I played around with setIndexWidget(), with QStyleOptionViewItem::displayPosition, Qt::SizeHintRole... everything! Nothing will work...
It's like the Trolls did that on purpose...
Oh my god... all I want is a QPixmap centered in a cell :crying:

durbrak
9th October 2006, 20:44
Oh and by the way..
I put a QSortProxyFilterModel between my model and my view... in my QTreeView I setIconSize() to some big values like 100x100 and when I click on the headers to sort something it works fine... but when i move the vertical scrollbar to the right until the decoration from the first column isn't visible anymore and click on the header again to sort something - each row gets small like 16px high and when i scroll back to the left my decoration-icon is cut off again...

jpn
9th October 2006, 20:48
Are you aware of Qt3Support module? You can always use Q3ListView if you're not satisfied with Qt 4's corresponding classes...



The Qt3Support module provides classes that ease porting from Qt 3 to Qt 4.

To include the definitions of the module's classes, use the following directive:

#include <Qt3Support>

To link against the module, add this line to your qmake .pro file:

QT += qt3support

durbrak
9th October 2006, 20:53
Yeah, I know about this. But 'downgrading' is not an option to me since I wanna make use of the (oh so mighty) Model/View-Framework because I wanna provide different view options to my users and I don't want to write 10 different itemwidgets.

jpn
9th October 2006, 21:11
Oh my god... all I want is a QPixmap centered in a cell :crying:
Just a pixmap? Like this?

durbrak
9th October 2006, 22:03
Yes! Kinda like this.
How'd you do that?

jpn
10th October 2006, 06:57
I used a custom delegate like this:


// a sensible maximum size for the icons
static const int MAX_HEIGHT = 100;
static const int MAX_WIDTH = 100;

void ItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QSize size = icon.actualSize(QSize(MAX_HEIGHT, MAX_WIDTH));
QRect rect = qApp->style()->alignedRect(qApp->layoutDirection(), Qt::AlignCenter, size, option.rect);
painter->drawPixmap(rect, icon.pixmap(QSize(MAX_HEIGHT, MAX_WIDTH)));
// draw selection here..?

// let the base class implementation draw focus?
QItemDelegate::drawFocus(painter, option, option.rect);
}

QSize ItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
return icon.actualSize(QSize(MAX_HEIGHT, MAX_WIDTH));
}