如何更改Qt qListView图标选择高亮显示

8
在使用图标模式下的qlistview时,当选择一个图标时,我需要完全去除高亮显示。使用下面的代码后,图标下方的文本不再高亮显示,但仍会在选中时出现蓝色颜色覆盖图标。
 QString stylesheet = "";
   stylesheet += "QListView::item:alternate {background-image: transparent; background-color: transparent;}";
   stylesheet += "QListView::item:selected {background-image: transparent; background-color: transparent;padding: 0px;color: black;}";
   stylesheet += "QListView::item:selected:active{background-image: transparent;background-color: transparent; color: black;}";
   stylesheet += "QListView::item:selected:!active{background-image: transparent;background-color: transparent;color: black;}";
   setStyleSheet(stylesheet);

有人知道如何在不必子类化QStandardItem的情况下更改图标上选择的颜色吗?

你使用的Qt版本是什么?我正在运行4.7.1,即使没有你的样式表,我也从未看到图标被突出显示。 - Liz
2个回答

13

对于使用QStandardItem的QListView,您可以实现所需的内容。只需创建一个图标,并为正常状态和选定状态添加相同的pixmap,然后在项目中设置setIcon。

QIcon icon;

icon.addPixmap(yourPixmap,QIcon::Normal);
icon.addPixmap(yourPixmap,QIcon::Selected);

qstandardItem.setIcon(icon);

我终于找到了答案!非常感谢!但是为了从其他地方移除FocusRect,这个答案对我有用 https://forum.qt.io/topic/87212/how-to-remove-blue-focus-rectangle/2 - DieChopper

0
找到了自己的答案。在QListview中,如果不使用委托,无法去除部分颜色叠加。但是,当我切换到使用QListWidget时,可以通过设置选定图标图像来禁用选择叠加。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接