不想在qlistWidget中点击/突出显示图标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不想在qlistWidget中点击/突出显示图标相关的知识,希望对你有一定的参考价值。
答案
您必须使用禁用QStyle::State_Selected
标志的委托:
from PySide2 import QtCore, QtGui, QtWidgets
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
option.state &= ~QtWidgets.QStyle.State_Selected
super(StyledItemDelegate, self).initStyleOption(option, index)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QListWidget()
delegate = StyledItemDelegate(w) # <---
w.setItemDelegate(delegate) # <---
w.setViewMode(QtWidgets.QListView.IconMode)
w.setIconSize(QtCore.QSize(128, 128))
w.setResizeMode(QtWidgets.QListView.Adjust)
for _ in range(20):
it = QtWidgets.QListWidgetItem()
it.setIcon(QtGui.QIcon("light.png"))
w.addItem(it)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
另一种选择是禁用QListWidgetItem的Qt::ItemIsSelectable
标志:
from PySide2 import QtCore, QtGui, QtWidgets
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion")
w = QtWidgets.QListWidget()
w.setViewMode(QtWidgets.QListView.IconMode)
w.setIconSize(QtCore.QSize(128, 128))
w.setResizeMode(QtWidgets.QListView.Adjust)
for i in range(20):
it = QtWidgets.QListWidgetItem(str(i))
it.setIcon(QtGui.QIcon("light.png"))
it.setFlags(it.flags() &~ QtCore.Qt.ItemIsSelectable) # <---
w.addItem(it)
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
以上是关于不想在qlistWidget中点击/突出显示图标的主要内容,如果未能解决你的问题,请参考以下文章