Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Centering Icon in QTreeWidgetItem

Tags:

qt

Trying to center an icon within a QTreeWidgetItem. The formatting set with setTextAlignment() only applies to text within the column. For example:

item = new QTreeWidgetItem(tree);

item->setIcon(0, QIcon(QPixMap(imageFile));
item->setTextAlignment(0, Qt::AlignHCenter | Qt::AlignVCenter);

tree->addTopLevelItem(item);

This will create a column with a left-aligned icon (and text center-aligned if there is any). Is there a way to center-align the icon with a custom stylesheet?

like image 726
h75 Avatar asked Oct 26 '25 16:10

h75


1 Answers

As far as I know, there is no simple solution to change the position of an icon within a QTreeWidgetItem.

As a workaround, you can maybe use setItemWidget and set as widget an instance of QLabel containing a well centered pixmap.

Another solution derivated from this answer could work, but is not trivial. If you create a new class child of QTreeWidgetItem and tweak the painting actions, you will maybe be able to draw the icon where you want. Very painfull in my opinion.

like image 173
Antwane Avatar answered Oct 28 '25 08:10

Antwane