h75
h75

Reputation: 43

Qt - Centering Icon in QTreeWidgetItem

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?

Upvotes: 4

Views: 3939

Answers (1)

Antwane
Antwane

Reputation: 22618

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.

Upvotes: 3

Related Questions