Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I using QWebPage as a delegate of QListView?
    primarykey
    data
    text
    <p>I'm working on a client for a social network. It is written in Qt &amp; Python. I need to put the timeline into a <code>QListView</code> (think about Twitter or Facebook).</p> <p>But, QListView can not render rich text, so I use <code>QTextDocument</code> as a delegate. </p> <pre><code>class HTMLDelegate(QtGui.QStyledItemDelegate): def paint(self, painter, option, index): options = QtGui.QStyleOptionViewItemV4(option) self.initStyleOption(options, index) options.text = options.text.replace(" ", "&amp;nbsp;") style = QtGui.QApplication.style() if options.widget is None else options.widget.style() doc = QtGui.QTextDocument() doc.setHtml(options.text) doc.setTextWidth(option.rect.width()) options.text = "" style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter) ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected #if (optionV4.state &amp; QStyle::State_Selected) #ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore() def sizeHint(self, option, index): options = QtGui.QStyleOptionViewItemV4(option) self.initStyleOption(options, index) doc = QtGui.QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QtCore.QSize(doc.idealWidth(), (doc.size().height())) </code></pre> <p>It is works, but it doesn't support all HTML labels.</p> <p>After a lot of search, I found <code>QWebView</code>, it is a fully-functional HTML viewer. I want to use <code>QWebPage</code> as a delegate for <code>QListView</code>.</p> <p>So, I tried:</p> <pre><code>class HTMLDelegate(QtGui.QStyledItemDelegate): def paint(self, painter, option, index): options = QtGui.QStyleOptionViewItemV4(option) self.initStyleOption(options, index) options.text = options.text.replace(" ", "&amp;nbsp;") style = QtGui.QApplication.style() if options.widget is None else options.widget.style() webPage = QtWebKit.QWebPage(self) webFrame = webPage.mainFrame() webPage.setViewportSize(webPage.mainFrame().contentsSize()) webFrame.setHtml(options.text) #doc.setTextWidth(option.rect.width()) options.text = "" style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter) #ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) webFrame.render(painter) painter.restore() </code></pre> <p>But it doesn't work. It just shows blank items.</p> <p>What wrong did I do?</p> <p>Or, is there another solution for that situation? I found <code>setItemWidget()</code> can put many widget into a item of <code>QListView</code>, but it will break Model-View.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload