Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i place QTableWidgetItem Icon in center of cell

Tags:

qt

qt4

pyqt4

i want a table cell to have just an icon without any text.

i see the QTableWidgetItem class has a method to align the text (int QTableWidgetItem::textAlignment () const)

i find no way to adjust the placement of the icon (which seems to get stuck on the left -- even where there is no text in the cell)

look at the Status and Energy columns.

alt text

like image 333
random guy Avatar asked Nov 10 '09 13:11

random guy


1 Answers

You can affect the position of the icon in relation to the text via the style options.

If the QTableWidgetItem is constructed without any text (via the constructor that does not accept a text argument), then the Qt::DisplayRole data item is not set and the text will not be displayed nor will it affect the icons display rectangle.

I was able to affect the position of the QTableWidgetItem's icon by subclassing the QTableWidget, overriding the viewOptions method and setting the decorationAlignment field of the view options, like this:

QStyleOptionViewItem MyTableWidget::viewOptions() const
{
    QStyleOptionViewItem option = QTableWidget::viewOptions();
    option.decorationAlignment = Qt::AlignHCenter | Qt::AlignCenter;
    option.decorationPosition = QStyleOptionViewItem::Top;
    ...
    return option;
}
like image 93
Avalanchis Avatar answered Nov 05 '22 20:11

Avalanchis