gpt4 book ai didi

python - 无法使用自定义 QStyledItemDelegate 从 QListView 中选择项目

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:20 25 4
gpt4 key购买 nike

我想用 HTML 代码呈现每一行。渲染有效,但 - 至少对我来说 - 无法单独选择项目。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

####################################################################
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())


list_data = [1,2,3,4]

####################################################################
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)

# create table
lm = MyListModel(list_data, self)
lv = QListView()
lv.setModel(lm)
lv.setItemDelegate(HTMLDelegate(self))

# layout
layout = QVBoxLayout()
layout.addWidget(lv)
self.setLayout(layout)

####################################################################
class MyListModel(QAbstractListModel):
def __init__(self, datain, parent=None, *args):
""" datain: a list where each item is a row
"""
QAbstractListModel.__init__(self, parent, *args)
self.listdata = datain

def rowCount(self, parent=QModelIndex()):
return len(self.listdata)

def data(self, index, role):
if index.isValid() and role == Qt.DisplayRole:
return QVariant(self.listdata[index.row()])
else:
return QVariant()

class HTMLDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
painter.save()

model = index.model()
record = model.listdata[index.row()]
doc = QTextDocument(self)
doc.setHtml("<b>%s</b>"%record)
doc.setTextWidth(option.rect.width())
ctx = QAbstractTextDocumentLayout.PaintContext()

painter.translate(option.rect.topLeft());
painter.setClipRect(option.rect.translated(-option.rect.topLeft()))
dl = doc.documentLayout()
dl.draw(painter, ctx)
painter.restore()


def sizeHint(self, option, index):
model = index.model()
record = model.listdata[index.row()]
doc = QTextDocument(self)
doc.setHtml("<b>%s</b>"%record)
doc.setTextWidth(option.rect.width())
return QSize(doc.idealWidth(), doc.size().height())
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
####################################################################
if __name__ == "__main__":
main()

最佳答案

我相信您应该做的是检测 HTMLDelegate.paint 方法中的选定项目,如果检测到此类项目,则用“突出显示”颜色填充背景。我稍微改变了你的绘画方法,请检查它是否适合你

def paint(self, painter, option, index):
painter.save()

# highlight selected items
if option.state & QtGui.QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight());

model = index.model()
record = model.listdata[index.row()]
doc = QTextDocument(self)
doc.setHtml("<b>%s</b>"%record)
doc.setTextWidth(option.rect.width())
ctx = QAbstractTextDocumentLayout.PaintContext()

painter.translate(option.rect.topLeft());
painter.setClipRect(option.rect.translated(-option.rect.topLeft()))
dl = doc.documentLayout()
dl.draw(painter, ctx)

painter.restore()

希望这对你有帮助,问候

关于python - 无法使用自定义 QStyledItemDelegate 从 QListView 中选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com