QTableWidget选择颜色
我希望选定的单元格具有不同的背景颜色。默认情况下,所选单元格中只有一条细下划线。
我已经尝试过这个:
table->setStyleSheet("QTableView {selection-background-color: #0000FF; selection-color: #00FF00;}
但它只会更改指针位于单元格上时显示的颜色。指针离开后,无论我通过table->selectRow(selRow)
选择单元格,都只有下划线。可能它在其他平台上看起来有所不同。
有很多具有相同主题的线程,但大多数答案是使用上面的样式表。没有任何效果,只有“移动颜色”发生变化。
预先感谢, 问候 马蒂亚斯
I want selected Cells to have a different background color. By default there is only a thin underline in the selected cell.
I've tried this:
table->setStyleSheet("QTableView {selection-background-color: #0000FF; selection-color: #00FF00;}
but it only changes the Color that is shown while the pointer is on a Cell. After the pointer is away, wether I select the cell by table->selectRow(selRow)
there is only the underline. Probably it looks differen on other plattforms.
There are a lot of thread with equal topic, but most answer is using the Stylesheet above. Nothing worked, only the "moseover Color" changes.
Thanks in advance, Regards
Matthias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
然后
table->setItemDelegateForColumn(2, new BackgroundDelegate(this));
then
table->setItemDelegateForColumn(2, new BackgroundDelegate(this));
这就是我所做的。
选择颜色用于选择一个项目,而项目焦点将为应突出显示的其余项目着色。
这适用于选定的单元格,就像选定的行一样。如果您想要“鼠标悬停”的内容,您可能必须在样式表中使用“悬停”。希望这能给你带来想法。
This is what I did.
The selection color does the one item that is selected while the item focus will color the rest of the items that should be highlighted.
This works for the selected cells like having a selected row. If you want something for "mouse over" you may have to use "hover" in the style sheet. Hope this can give you ideas.
不幸的是,似乎没有“nofocus”属性,因此您只需为所有选定项目设置颜色,然后将聚焦颜色覆盖回默认值。
#3399FF
是颜色选择器显示的默认突出显示背景颜色是我的设置,所以我使用了它。你可以用你喜欢的任何颜色来代替。当选择失去焦点时,
color: #FFFFFF
将文本颜色设置为自定义颜色。当我聚焦时它对我来说是白色的,所以当它失去焦点时我只是将其保持为白色。您可以使用您喜欢的任何颜色,也可以删除该部分以使用默认颜色。Unfortunately there doesn't seem to be a "nofocus" property, so you just have to set the colour for all selected items, then override the focused colour back to the default.
#3399FF
is what a colour picker revealed the default highlight background colour was for my setup, so I used that. You can substitute with whichever colour you like.The
color: #FFFFFF
sets the text colour to something custom when the selection loses focus. It's white for me when I have focus, so I just keep it as white when it loses focus. You can use whatever colour you like, or remove that part to use the default.您需要使用自定义委托来根据需要绘制选定的单元格。
查看 QAbstractItemView::setItemDelegate() 方法和 QItemDelegate 类。您需要重写
QItemDelegate::paint()
方法。 Paint 方法采用QStyleOptionViewItem
结构 - 您可以使用它来确定是否选择了要求您绘制的项目。QItemDelegate::paint
的 Qt 文档具有完全执行此操作的示例代码。You need to use a custom delegate to paint selected cells as you want them.
Have a look at the
QAbstractItemView::setItemDelegate()
method and theQItemDelegate
class. You'll need to override theQItemDelegate::paint()
method. The paint method takes aQStyleOptionViewItem
structure - you can use this to determine if the item you're being asked to paint is selected.The Qt docs for
QItemDelegate::paint
have sample code that does exactly that.