Qt - Center icon in QTreeWidgetItem
Trying to center an icon in a QTreeWidgetItem. The formatting set using setTextAlignment () applies only to the 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 centered on the text, if any). Is there a way to center align the icon using a custom stylesheet?
source to share
As far as I know, there is no easy solution to change the position of the icon in the QTreeWidgetItem.
As a workaround, you can use setItemWidget and set the widget to a QLabel instance containing a well-centered pixmap.
Another solution derived from this answer may work, but is not trivial. If you create a new child class, QTreeWidgetItem, and set up the actions on the picture, you can probably draw the icon wherever you want. Very painful, in my opinion.
source to share