pyQt 与交互式 SVG 图像 [英] pyQt with interactive SVG images

查看:95
本文介绍了pyQt 与交互式 SVG 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何将交互式 SVG 文件添加到 QGraphicsScene?我可以添加交互式 SVG 文件,但它只是显示为静态图像.我正在阅读有关 QGraphicsWebView 的内容,但无法弄清楚如何向其中添加 SVG 文件并且缺少示例.

How would I go about adding an interactive SVG file to a QGraphicsScene? I can add an interactive SVG file, but it's just displayed as a static image. I was reading about QGraphicsWebView but can't figure out how add SVG files to it and examples are lacking.

让我更具体地说明我在寻找什么.假设我有一个绘制框的 SVG 文件.当我将鼠标悬停在那个框上时,我想改变颜色以更改触发悬停事件.我必须编辑文件并重新绘制图像吗?感觉应该有一种方法可以用 Qt 处理交互式 SVG 文件.

Let me be a little more specific with what I'm looking for. Let's say I have a SVG file that draws a box. When I mouse over that box I want to color to change triggered off a hover event. Do I have to edit the file and redraw the image? Feels like there should be a way of doing interactive SVG files with Qt.

推荐答案

我遇到了同样的问题,您的问题为我指明了正确的方向(使用 QGraphicsWebView()).解决方法其实很简单:

I had the same problem and your question pointed me in right direction (using QGraphicsWebView()). Solution is actually very simple:

import sys
from PyQt4 import QtCore, QtGui, QtSvg
from PyQt4.QtWebKit import QGraphicsWebView
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    scene = QtGui.QGraphicsScene()
    view = QtGui.QGraphicsView(scene)

    br = QtSvg.QGraphicsSvgItem("C:\your_interactive_svg.svg").boundingRect()

    webview = QGraphicsWebView()
    webview.load(QtCore.QUrl("C:\your_interactive_svg.svg"))
    webview.setFlags(QtGui.QGraphicsItem.ItemClipsToShape)
    webview.setCacheMode(QtGui.QGraphicsItem.NoCache)
    webview.resize(br.width(), br.height())

    scene.addItem(webview)
    view.resize(br.width()+10, br.height()+10)
    view.show()
    sys.exit(app.exec_())

它非常适合我(脚本和其他东西).

It works perfect for me (scripting and other stuff).

如您所见,我也将我的 svg 加载为 QGraphicsSvgItem,因为我不知道其他获取 svg 大小的方法.

As you can see, I've also loaded my svg as QGraphicsSvgItem, because I didn't know other way to get size of my svg.

这篇关于pyQt 与交互式 SVG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆