[PyKDE] Implementing QSimpleRichText

David Boddie david at boddie.org.uk
Fri Aug 13 18:58:43 BST 2004


On Thu, 12 Aug 2004 21:02:46, Eron Lloyd wrote:

>Does anybody have an example of how to implement rich text inside of a 
>QListViewItem? I can't wrap my head around the C++ examples I've found enough 
>to translate what needs to be done. It seems a little more tricky when not 
>implemented inside a QWidget...

You could probably start from the example given at

    http://doc.trolltech.com/qq/qq08-fancy-list-view.html

Something simple like the following might be enough, but you may want to
think about how the items are painted.

Good luck,

David


#!/usr/bin/env python

import sys

from qt import QApplication, QBrush, QLabel, QListView, QListViewItem, \
               QPixmap, Qt

class RichTextItem(QListViewItem):

    def __init__(self, parent, previous = None):
    
        QListViewItem.__init__(self, parent, previous)
        self.label = QLabel(None)
    
    def paintCell(self, painter, colorGroup, column, width, alignment):
    
        painter.save()
        painter.fillRect(0, 0, width, self.height(), QBrush(Qt.white))
        self.label.setText(self.text(column))
        self.label.resize(width, self.height())
        pixmap = QPixmap.grabWidget(self.label, 0, 0, width, self.height())
        painter.drawPixmap(0, 0, pixmap)
        painter.restore()

example_text = """<h1>My list items</h1>
<b>A bold item</b>
<i>An italic item</i>
Some <i>mixed</i> <b>text</b>"""

if __name__ == "__main__":

    app = QApplication(sys.argv)
    list_view = QListView()
    list_view.addColumn("Label")
    list_view.setSorting(-1)
    
    previous = None
    for line in example_text.split("\n"):
    
        list_item = RichTextItem(list_view, previous)
        list_item.setText(0, line)
        previous = list_item
    
    list_view.show()
    
    app.setMainWidget(list_view)
    sys.exit(app.exec_loop())



___________________________________________________________
$0 Web Hosting with up to 120MB web space, 1000 MB Transfer
10 Personalized POP and Web E-mail Accounts, and much more.
Signup at www.doteasy.com




More information about the PyQt mailing list