Hello,
I'm writing here because I think I will be off-topic in the QtProgramming section.
I asked the same question to the Italian QtForum but I haven't received any answer.
I'm developing a simple application with in Python + PyQt. There is a QMainWindow that hosts a QWebView. This shows an html page loaded through the .setHtml method.
In the html code there is a Javascript that draws a png image, in detail it assigns this png to a google maps marker.
I need to provide the png image using the QtWebKit Bridge because I want to elaborate it sometime. I'm currently using the QtWebKit Bridge to pass other variables.
This is an executable example code:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys
app = QApplication(sys.argv)
html = '''
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript">
function loadImage() {
_XPData.getPixmap.assignToHTMLImageElement(document.getElementById("imageElement"));
//document.getElementById("imageElement").src = "http://maps.google.com/mapfiles/kml/pal2/icon56.png"
}
</script>
</head>
<body onload="loadImage()">
<img id="imageElement"/>
</body>
</body>
</html>
'''
class XPData(QObject):
def __init__(self):
QObject.__init__(self)
self.icon = QPixmap('icon56.png')
print self.icon.size()
def getPixmap(self):
return self.icon
getPixmap = pyqtProperty('QPixmap', getPixmap)
class AppForm(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.xpout = XPData()
self.webView = QWebView()
self.setCentralWidget(self.webView)
self.webView.setHtml(html)
self.webView.page().mainFrame().addToJavaScriptWindowObject('_XPData', self.xpout)
form = AppForm()
form.show()
app.exec_()
|
To test it, place in the execution folder a png image (you could use the same icon56.png downloaded from the link provided).
If you comment out the line with "assignToHTMLImageElement" and remove the comment from the next one, you will see the image. So the html code is ok.
Anyway, I want to use the toDataURL() but it doesn't work too. Instead, if I pass a number (for example a float rather than a QPixmap) the javascript receive correctly the parameter.
Please, may you help me?
Thanks in advance
Marco