PDA

View Full Version : :( Problems with QTreeWidgetItem icons



davit
16th April 2007, 10:24
Hi all,

If I try to add no square shaped QIcon object to QTreeWidgetItem it doesn't displayed on item. Who is know the workaround for this. Please help me.

Thanks in advance,
--davit

wysota
16th April 2007, 10:41
The shape doesn't matter. Can we see your code?

guilugi
16th April 2007, 10:49
I confirm, I just tried to display a 64x20 icon in my treewidget...and it doesn't appear!

It works in Qt Designer though..

wysota
16th April 2007, 11:17
My guess is that probably you provided a relative path to the image which is incorrect runtime and the pixmap can't be created. Could you please show us the code?

guilugi
16th April 2007, 11:23
Well, in my application, I use a resource file.
You can find in it (amongst others), a squared PNG image (64x64), that I always use as an icon for my tree items.

I simply resized it to 64x20 with mogrify, recompiled, and then...no more icons...

I checked the tracker, and I didn't find something relevant...

marcel
16th April 2007, 11:27
Perhaps the saved file cannot be interpreted by Qt. ( not standard PNG ).
I'm just saying...

guilugi
16th April 2007, 11:32
I thought of that first, but since it's correctly displayed in QtDesigner, I think that's some damn good PNG :D

Really annoying if it's a bug...there may be an easy workaround by making square pixmaps with transparent zones.

wysota
16th April 2007, 11:50
Could you just please show some code? It's not a bug, I assure you. I have used non-rectangular icons (define "non-rectangular" anyway... every image is rectangular) just fine many times.

davit
16th April 2007, 12:04
Hi wysota,

The code is simple. I have implemented my own tree_widget_item class which is inherited from QTreeWidgetItem. The constructor is following:


tree_widget_item(QTreeWidget* t, const QString& s, const QPixmap& p)
QTreeWidgetItem(t, QStringList(s))
{
setIcon(QIcon(p));
}
p is correct pixmap, because if I use it for giving icon to comboBox it displays correctly.

Note: I didn't said that icon has no rectangular shape, I said that icon has no square shape.
By saying icon in this case I mean QPixmap object, from which I'm trying to create QIcon.
For rectangular Icons it works unstable (in some cases it displays, in other ones not).

Thanks,
--davit

wysota
16th April 2007, 16:58
Could you provide a little more code? Something that compiles, maybe? If you're able to reproduce the problem on a small example, it could help to solve the issue.

This seems to work fine for me (with a non-square pixmap).


#include <QApplication>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QtDebug>

int main(int argc, char **argv){
QApplication app(argc, argv);
QTreeWidget wgt;
wgt.setColumnCount(1);
QTreeWidgetItem *item = new QTreeWidgetItem(&wgt);
QPixmap px("someimage.jpg");
qDebug() << px.size();
item->setIcon(0, QIcon(px));
wgt.show();
return app.exec();
}

davit
18th April 2007, 11:26
Hi wysota.

Your code doesn't work for attached .xpm file (icons doesn't displayed on treeWidget items).

Note: I'm using Qt4.2.2 on RHEL3.0.

Thanks,
--davit

davit
18th April 2007, 11:45
Sorry I can't attach my file. So I'll copy the content to here.


/* XPM */
static char *dummy[]={
"150 1 1 1",
". c #000000",
".

wysota
18th April 2007, 13:27
It works just fine. It is just being scaled down so that it can fit into the item. Try a 25x10 image or change the icon size.

davit
18th April 2007, 13:38
Thanks guys for replays.
I have found solution. It seems QTreeWidget requires to set to icon that size which has QPixmap object.
So, we need to do following:


QTreeWidget* t = new QTreeWidget;
QPixmap p("somefile.xpm");
t->setIconSize(p.size());
QTreeWidgetItem* item = new QTreeWidgetItem(t);
item->setIcon(QIcon(p));

Best regards,
--davit