0

私はソーシャルネットワークのクライアントに取り組んでいます。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が壊れます。

4

1 に答える 1

0

わかりました、私が提案したあなたの最初の問題は、あなたがwebPage.setViewportSize以前に電話していたことでしたwebFrame.setHtml

sizeHintトリッキーです 。QWebViewもちろん、Web ブラウザの仕組みは、ユーザーが設定したビューポート サイズに従ってレイアウトを実行することです。自分で幅の適切な値を考え出す必要があることをお勧めします。からフレームの高さを取得できるはずcontentsSizeです。

于 2013-02-21T10:09:33.347 に答える