2010-06-29 17 views
1

我想在代理中使用QGraphicWebView來呈現QTableView單元格,但我只是不知道如何處理paint()方法需要的QStyleOptionGraphicsItem參數。如何建立它/我應該在哪裏找回它? 我使用this code作爲參考,所以paint()方法應該是這樣的:如何使用QGraphicsWebView?

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() 

有什麼建議?

+0

是否有一個原因,你使用'QGraphicsWebView'而不是'QWebView'? – 2010-06-29 15:30:31

+0

不是真的,我認爲它可能是一個更好的選擇,因爲它有一個繪製方法:P – 2010-07-01 11:08:02

回答

0

我假設你並不需要QGraphicsWebViewQWebView就足夠了。

請務必記住,您不需要親自致電QWidget::paintEvent()。考慮到這個約束,你會想要使用一個可以在繪製設備上渲染的助手類,或者使用給定的畫家渲染。 QWebFrame以其render 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() 
+0

謝謝,我的setwidth()部分並不是真的設置頁面大小,但渲染部分工作正常。 – 2010-07-01 11:24:22