View Full Version : How to change the QTreeWidgetItem background images on selected event in style sheet.

2nd June 2014, 05:44
Dear All,

I am newbie in qt gui development, i have created the QTreeWidget Class and added the QTreeWidgetItems liks,

class xyz: public QTreeWidget

xyx (QWidget *inParent = 0 );

QTreeWidgetItem *aItem;

in c file i have created the QTreeWidgetItem like this

QTreeViewItem *a1 = new QTreeWidgetItem(this);
a1->setBackground = (0,QBrush(QImage(":/Images/001.png")));

QTreeViewItem *a2 = new QTreeWidgetItem(this);
a2->setBackground = (0,QBrush(QImage(":/Images/003.png")));

the images are displayed successfully, in the run time when the a2item selected the 003.png image will display in the a1 and when the a2 item selected the 004.png image will display i am using style sheet.

please give suggestions for me to display the different images in the item when the item selected.

sorry for the bad english.

Thanks in advance.

2nd June 2014, 13:25
Try below style sheet in which you can replace background-color with image.
a1->setStyleSheet(" QTreeWidget::item { background-color: yellow; } QTreeWidget::item:selected { background-color: blue; }");

3rd June 2014, 04:59
Thanks for your reply,

One mistake in my code

QTreeViewItem *a1 = new QTreeWidgetItem(this);
a1->setBackground = (0,QBrush(QImage(":/Images/001.png")));

it should be

QTreeWidgetItem *a1 = new QTreeWidgetItem(this);
a1->setBackground = (0,QBrush(QImage(":/Images/001.png")));

in QTreeWidgetItem there is no memeber function called setStyleSheet so how can we fix this issue.

Thanks in advance

3rd June 2014, 06:57
Remove your setBackground function call it is not required if you are calling setStyleSheet function on widget.
Try calling setStyleSheet function as I said and it should work.