View Full Version : Centering An Icon In A QTableWidgetItem?

14th November 2007, 22:53
My application (using 4.2.2) has a QTableWidget in which one of the cells displays an icon. This cell contains no text or other widgets. The icon is left-justified but would look better if it were centered.

What must I do to center the icon in the QTableWidgetItem?

14th November 2007, 23:40
First thing to try would be to set Qt::TextAlignmentRole for the item to Qt::AlignCenter.

14th November 2007, 23:55
... set Qt::TextAlignmentRole for the item to Qt::AlignCenter.

Using Qt::AlignCenter doesn't center the icon.

This is my code; note that TableCell subclasses QTableWidgetItem (it only adds the '<' operater to accomplish a sort via tooltip text).

setItem( m_nCurrRow, COL_Status, new TableCell( "" ) );
item( m_nCurrRow, COL_Status )->setTextAlignment( Qt::AlignCenter );

It appears that there is no easy way to do this.

15th November 2007, 00:11
In that case you need to provide a custom delegate for the table and paint the image centered (you can have a custom role for deciding whether the image should be centered - you might call it DecorationAlignmentRole or reuse TextAlignmentRole if you are sure there will be no text in that cell).