0

I'm trying to get the hover highlight color I set on a QListView applied to the items' icons. Right now it only applies to the surroundings (the image has an alpha channel which is 0 where there is white background):

enter image description here

You can see clearly that the 3D model itself is not affected by the highlight color. When I select the item it the image gets the selection color:

enter image description here

And after being selected it is also affected by the highlight color (i.e. that's the result I want to produce by only hovering over the item):

enter image description here

The stylesheet I use is the following:

QListView::item {
    color: #000000;
    background-color:transparent;
}

QListView::item:hover:!pressed {
    padding: -1px;
    border: 1px solid #75c1ff;
    border-radius: 2px;
    background-color: #c6e5ff;
}

QListView::item:selected {
    padding: -1px;
    border: 1px solid #5eb6ff;
    border-radius: 2px;
    background-color: #9ed2ff;
}

This is how I'm returning the image in the data method of the item model of the list view:

return QIcon(QPixmap::fromImage(m_renderedObjectsModels.value(objectModel.path())));

I tried to add multiple QPixmaps there for the different states (like suggested here) but none are applied to only hovering (at least I didn't find a state that I could add a pixmap for that would make it work).

4

1 回答 1

1

我不确定您使用的是哪个 Qt 版本,所以让我们考虑一些 Qt5.15。当您使用样式表时,可能真正的绘图发生在具有 CE_ItemViewItem 类型的QCommonStyle::drawControl()中。如果是这种情况,那么事情就在绘制顺序中:

  • 背景
  • 复选标记
  • 图标
  • 文本
  • 焦点矩形

所以我认为 #2 和 #3 图像是这样绘制的,因为在图标之后绘制了“焦点矩形”。这不会发生在 #1 的情况下,当仅在其上绘制带有图标但没有“焦点矩形”的背景时。

要解决这个问题,您可以尝试重新实现项目的样式和绘图。或者更肮脏的方法可能是设置opt参数的 QStyle::State_HasFocus 标志。也许你可以在项目的委托中的某个地方做到这一点。

于 2021-05-11T11:05:16.503 回答