如何使用QGraphicsWebView?

时间:2010-06-29 15:21:54

标签: html pyqt

我想在委托中使用QGraphicWebView来呈现QTableView单元格,但我不知道如何处理paint()方法所需的QStyleOptionGraphicsItem参数。如何构建它/我应该在哪里检索它? 我使用this code作为参考,因此paint()方法应该是这样的:

def paint(self, painter, option, index):
    web = QGraphicsWebView()
    web.setHtml(some_html_text)
    web.page().viewportSize().setWidth(option.rect.width())
    painter.save()
    painter.translate(option.rect.topLeft());
    painter.setClipRect(option.rect.translated(-option.rect.topLeft()))
    web.paint(painter, ??????) # what here?
    painter.restore()

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我假设你真的不需要QGraphicsWebView而且QWebView就足够了。

请务必记住,您不应该自己致电QWidget::paintEvent()。给定该约束,您将需要使用可以在绘制设备上渲染或使用给定的绘制器渲染的辅助类。 QWebFramerender function的形式提供了一种此类方法。基于您的链接示例,以下内容应该有效:

class HTMLDelegate(QStyledItemDelegate):

    def paint(self, painter, option, index):
        model = index.model()
        record = model.listdata[index.row()]

        # don't instantiate every time, so move this out
        # to the class level
        web = QWebView() 
        web.setHtml(record)
        web.page().viewportSize().setWidth(option.rect.width())

        painter.save()
        painter.translate(option.rect.topLeft());
        painter.setClipRect(option.rect.translated(-option.rect.topLeft()))
        web.page().mainFrame().render(painter)
        painter.restore()
相关问题