2010-08-12 69 views
2

我想用HTML代碼呈現每一行。渲染工程,但至少對我來說,項目不能單獨選擇。無法從QListView中自定義項目QStyledItemDelegate

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() 

回答

2

我相信你應該做的是檢測所選項目在HTMLDelegate.paint方法,如果這些物品檢測填充「高亮」顏色的背景。我稍微改變你的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() 

希望這會有所幫助,至於

+0

只是coppied的#亮點選定的項目,它解決了我的問題。真的很好。 – Rodrigo 2010-12-11 13:08:57

相關問題