简体   繁体   中英

How to set background and foreground color of QTableWidget row Vertical Header Item

I am trying to set the background and foreground color of a QTableWidgetItem that is a Vertical Header Item in a row of my QTableWidget . The following code does not work for a QTableWidgetItem that is a vertical header item, even though the same code works fine for a QTableWidgetItem that is a regular cell.

I'm using Qt 4.8 on Fedora 17 x64 Gnome 3.4

static const QBrush AddedCellBackground = Qt::yellow;
static const QBrush AddedCellForeground = Qt::red;

void rowSelected()
{
    QTableWidgetItem *vertHeadItem = _getSelectedItemVerticalHeader();
    vertHeadItem->setForeground( AddedCellForeground );
    vertHeadItem->setBackground( AddedCellBackground );
}

This code makes no change when vertHeadItem is a vertical header item, but it has the desired affect when it is a regular cell. How can I set the background and foreground of a vertical header item?

I think you can easily target specific items by knowing it's "AccessibleName", you can try this:

QTableWidgetItem#YourAccessibleName {

/* style definitions */

}

In the question Is it possible to change the colour of a QTableWidget row label? about the same topic but for PyQt my solution was to get the QTableWidgetItem of the vertical header and set the background and foreground there, however only the foreground was changed, the background stayed a smooth gray gradient. Probably the style was overridding/ignoring the background. Specifying a different style (Cleanlooks for example) fixed it.

So add something like in the construction of the table widget

QTableWidget::verticalHeader().setStyle(QStyleFactory::create("CleanLooks"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM