开发者

PyQt4.QtWebKit + Background Transparency

If you have a compositing window manager, this will give you a transparent window:

import PyQt4.QtGui
a = PyQt4.QtGui.QApplication([])
import PyQt4.QtCore
import PyQt4.QtWebKit
w = PyQt4.QtWebKit.QWebView()
w.setAttribute(PyQt4.QtCore.Qt.WA_TranslucentBackground)
w.show()
a.exec_()

But this will not have a transparent window:

import PyQt4.QtGui
a = PyQt4.QtGui.QApplication([])
import PyQt4.QtC开发者_StackOverflow中文版ore
import PyQt4.QtWebKit
w = PyQt4.QtWebKit.QWebView()
w.setAttribute(PyQt4.QtCore.Qt.WA_TranslucentBackground)
w.page().currentFrame().documentElement().setInnerXml("text")
w.show()
a.exec_()

I want to be able to have text floating over the background... Is this possible?


This seems to work:

from PyQt4 import QtGui, QtWebKit
from PyQt4.QtCore import Qt
a = QtGui.QApplication([])
view = QtWebKit.QWebView()
view.setAttribute(Qt.WA_TranslucentBackground)
# Works with and without that following line
view.setAttribute(Qt.WA_OpaquePaintEvent, False)
page = view.page()
palette = page.palette()
palette.setBrush(QtGui.QPalette.Base, Qt.transparent)
page.setPalette(palette)
page.currentFrame().documentElement().setInnerXml("text")
view.show()
a.exec_()

Source: http://labs.qt.nokia.com/2009/06/30/transparent-qwebview-or-qwebpage/


The problem here is lazy instantiation. Initially, the WebKit view is not instantiated, and so you get a transparent background. As soon as you access w.page(), the WebKit view is actually instantiated, and so the default white background appears. I am not aware of any way of getting transparency then from inside the WebKit view; there may be a way, but it's not "normal web stuff". For example, w.page().currentFrame().documentElement().setInnerXml('<style>html,body{background:transparent}</style>') doesn't help: the default of white is rendered underneath the transparency.

Possible help: Is it possible to render web content over a clear background using WebKit?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜