Python 无法使用自定义QStyledItemDelegate从QListView中选择项
我想用HTML代码呈现每一行。渲染工作正常,但不能单独选择项目(至少对我来说是这样)Python 无法使用自定义QStyledItemDelegate从QListView中选择项,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,我想用HTML代码呈现每一行。渲染工作正常,但不能单独选择项目(至少对我来说是这样) import sys from PyQt4.QtCore import * from PyQt4.QtGui import * #################################################################### def main(): app = QApplication(sys.argv) w = MyWindow()
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()
我认为您应该做的是在htmlelegate.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()
希望这对我有所帮助,因为我刚刚完成了突出显示的选定项目,它解决了我的问题。真的很好。