我希望选定的单元格具有不同的背景色。默认情况下,选定的单元格中只有一条细下划线。
我试过了:
table->setStyleSheet("QTableView {selection-background-color: #0000FF; selection-color: #00FF00;}但它仅更改指针位于单元格上时显示的颜色。指针离开后,无论我是否按table->selectRow(selRow)选择单元格,都只有下划线。可能在其他平台上看起来有所不同。
有很多主题相同的主题,但大多数答案是使用上面的样式表。什么都不起作用,只有"moseover Color“改变了。
提前感谢,问候Matthias
发布于 2014-08-06 18:33:58
class BackgroundDelegate : public QStyledItemDelegate {
public:
explicit BackgroundDelegate(QObject *parent = 0)
: QStyledItemDelegate(parent){}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
// Fill the background before calling the base class paint
// otherwise selected cells would have a white background
QVariant background = index.data(Qt::BackgroundRole);
if (background.canConvert<QBrush>())
painter->fillRect(option.rect, background.value<QBrush>());
// the comment below makes selection transparent
//QStyledItemDelegate::paint(painter, option, index);
// To draw a border on selected cells
if(option.state & QStyle::State_Selected) {
painter->save();
QPen pen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
int w = pen.width()/2;
painter->setPen(pen);
painter->drawRect(option.rect.adjusted(w,w,-w,-w));
painter->restore();
}
}
};然后是table->setItemDelegateForColumn(2, new BackgroundDelegate(this));
发布于 2014-08-06 20:18:18
这就是我所做的。
stylesheet = "QTableView{selection-background-color: " + highlight + ";"
stylesheet += "selection-color: white; show-decoration-selected: 10}\n"
stylesheet += "QTableView::item:focus{border: 1px solid yellow;"
stylesheet += "background-color:"+highlight+"}"
table->setStyleSheet(stylesheet);选择颜色用于选择一个项目,而项目焦点将对应突出显示的其余项目进行着色。
这适用于选定的单元格,就像具有选定的行一样。如果你想要“鼠标悬停”的东西,你可能不得不在样式表中使用"hover“。希望这篇文章能给你一些启发。
发布于 2016-05-18 06:05:43
table->setStyleSheet("QTableView:item:selected {background-color: #XXYYZZ; color: #FFFFFF}\n"
"QTableView:item:selected:focus {background-color: #3399FF;}")不幸的是,似乎没有"nofocus“属性,所以你只需要为所有选中的项目设置颜色,然后覆盖聚焦的颜色回到默认值。#3399FF是颜色选择器显示我的设置的默认高亮背景色,所以我使用了它。你可以用你喜欢的任何颜色来代替。
当所选内容失去焦点时,color: #FFFFFF会将文本颜色设置为自定义颜色。当我有焦点时,它对我来说是白色的,所以当它失去焦点时,我只是保持它为白色。您可以使用任何您喜欢的颜色,或删除该部分以使用默认颜色。
https://stackoverflow.com/questions/5587709
复制相似问题