I'm trying to display some Wikipedia articles containing math in SVG
images
in QtWebView, though I can't figure out how to set width
and height
attributes of the image. QtWebView
will ignore these attributes and when I
omit them the image is very small.
You can test it as follows: Download this HTML editor. Go to this web page and download e.g. this SVG image.
Create img element, reflecting the styling in Wikipedia:
<img src="e15d3619d42b28662c5952c9ece60cd928e31774.svg" style="vertical-align: -0.671ex; width:25.517ex; height:2.343ex;">
And the image will be ignored.
The following works, but the image is very small.
<img src="e15d3619d42b28662c5952c9ece60cd928e31774.svg" >
How to display the image with the right size (width and height)?
Try to use this HTML approach for changing an SVG image dimensions:
<!-- you can test this code in any online html editor. -->
<html>
<head>
<!-- blah blah blah... -->
</head>
<body>
<p>all the images must be redrawn properly when SVG animation runs...</p>
<img height="250px" width="800px" border="5" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/e15d3619d42b28662c5952c9ece60cd928e31774">
</body>
</html>
Very often SVG height or width are incorrectly calculated in WebKit browsers. To solve this problem use this CSS style:
svg { width: 100%; height: auto; }
I've recently found quite useful and interesting post: CSS Tricks: How to Scale SVG.
Or try this Python approach for changing an SVG image dimensions described Here.
from PyQt5 import QtCore, QtGui, QtSvg
from PyQt5.QtWebKit import QGraphicsWebView
import sys
application = QtGui.QApplication(sys.argv)
myScene = QtGui.QGraphicsScene()
myView = QtGui.QGraphicsView(myScene)
box = QtSvg.QGraphicsSvgItem('/Users/swift/Desktop/myImage.svg').boundingRect()
webView = QGraphicsWebView()
webView.load(QtCore.QUrl('/Users/swift/Desktop/myImage.svg'))
webView.setFlags(QtGui.QGraphicsItem.ItemClipsToShape)
webView.resize(box.width(), box.height())
incWidth = 48
incHeight = 36
myScene.addItem(webView)
myView.resize(box.width() + incWidth, box.height() + incHeight)
myView.show()
sys.exit(app.exec_())
And, of course, you can call setHtml()
public function with argument:
code =
"""
<html>
<head>
</head>
<body>
<!-- You can load SVG image itself. -->
<img height="250px" width="800px" style="width: 100%;" src="myImage.svg">
<!-- Or you can load SVG Viewport and use other images inside it. -->
<svg width="600" height="450">
<image href="myImage.svg" x="0" y="0" height="320px" width="240px"/>
</svg>
</body>
</html>
"""
self.webView.setHtml(code)
Or assign the size directly via Qt:
svgImage = QImage(QSize(1280, 720))
Also look at official Qt SVG Documentation
describing XML-based approach Here.