1

我试图SVG在 QtWebView 的图像中显示一些包含数学的维基百科文章,但我不知道如何设置图像widthheight属性。QtWebView将忽略这些属性,当我忽略它们时,图像非常小。

您可以按如下方式对其进行测试: 下载HTML 编辑器。转到网页并下载例如SVG 图像。

创建 img 元素,反映 Wikipedia 中的样式:

<img src="e15d3619d42b28662c5952c9ece60cd928e31774.svg" style="vertical-align: -0.671ex; width:25.517ex; height:2.343ex;">

并且图像将被忽略。

以下工作,但图像非常小。

<img src="e15d3619d42b28662c5952c9ece60cd928e31774.svg" >

如何以正确的尺寸(宽度和高度)显示图像?

4

2 回答 2

2

尝试使用这种 HTML 方法来更改 SVG 图像尺寸:

<!-- 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>

很多时候 SVG 的高度或宽度在 WebKit 浏览器中计算不正确。要解决此问题,请使用以下 CSS 样式:

svg { width: 100%; height: auto; }

我最近发现了一篇非常有用且有趣的帖子:CSS Tricks: How to Scale SVG

或者尝试这种 Python 方法来更改此处描述的 SVG 图像尺寸。

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

而且,当然,您可以setHtml()使用参数调用公共函数:

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)

或者直接通过 Qt 分配大小:

svgImage = QImage(QSize(1280, 720))

另请查看官方Qt SVG Documentation描述的基于 XML 的方法Here

于 2017-06-29T15:38:47.057 回答
0

使用其他单位作为高度和宽度。例如:

<img src="https://www.w3.org/Icons/SVG/svg-logo.svg" height='200px' />
于 2017-06-29T11:33:20.217 回答