编程语言
首页 > 编程语言> > python-无法使用自定义QStyledItemDelegate从QListView中选择项目

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

作者:互联网

我想用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()

希望这会有所帮助,问候

标签:pyqt,pyqt4,python
来源: https://codeday.me/bug/20191209/2097262.html