私はソーシャルネットワークのクライアントに取り組んでいます。Qt&Pythonで書かれています。QListView
タイムラインを(TwitterやFacebookについて考えてください)に入れる必要があります。
ただし、QListViewはリッチテキストをレンダリングできないためQTextDocument
、デリゲートとして使用します。
class HTMLDelegate(QtGui.QStyledItemDelegate):
def paint(self, painter, option, index):
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
options.text = options.text.replace(" ", " ")
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 & 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()))
動作しますが、すべてのHTMLラベルをサポートしているわけではありません。
何度も検索したところQWebView
、完全に機能するHTMLビューアであることがわかりました。QWebPage
の代理人として使用したいQListView
。
だから、私は試しました:
class HTMLDelegate(QtGui.QStyledItemDelegate):
def paint(self, painter, option, index):
options = QtGui.QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
options.text = options.text.replace(" ", " ")
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()
しかし、それは機能しません。空白のアイテムが表示されるだけです。
私は何を間違えましたか?
または、その状況に対する別の解決策はありますか?setItemWidget()
のアイテムに多くのウィジェットを入れることができることがわかりましたがQListView
、Model-Viewが壊れます。