4

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?

h75
  • 43
  • 1
  • 3

1 Answers1

3

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.

Community
  • 1
  • 1
Antwane
  • 20,760
  • 7
  • 51
  • 84