简体   繁体   English

如何设置QTableWidget行垂直标题项的背景和前景色

[英]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 . 我试图在QTableWidgetItem的一行中设置QTableWidget的背景和前景色,该颜色是Vertical Header Item。 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. 以下代码不适用于作为垂直标头项的QTableWidgetItem ,即使相同的代码对于作为常规单元格的QTableWidgetItem也可以正常工作。

I'm using Qt 4.8 on Fedora 17 x64 Gnome 3.4 我在Fedora 17 x64 Gnome 3.4上使用Qt 4.8

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. vertHeadItem是垂直标题项目时,此代码不会发生任何变化,但当它是常规单元格时,它将具有所需的效果。 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: 我认为您可以通过知道它的“ AccessibleName”来轻松定位特定项目,可以尝试以下操作:

QTableWidgetItem#YourAccessibleName {

/* style definitions */

}

In the question Is it possible to change the colour of a QTableWidget row label? 问题中是否可以更改QTableWidget行标签的颜色? 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. 关于同一主题,但对于PyQt,我的解决方案是获取垂直标头的QTableWidgetItem并在此处设置背景和前景,但是仅更改前景,背景保持平滑的灰色渐变。 Probably the style was overridding/ignoring the background. 样式可能覆盖/忽略了背景。 Specifying a different style (Cleanlooks for example) fixed it. 指定其他样式(例如,Cleanlooks)将其修复。

So add something like in the construction of the table widget 因此,在表格小部件的构造中添加类似内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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