svg - 使用 PyQT 中的 QSvgWidget 全屏设置 SVG 图像的尺寸?

标签 svg pyqt qtsvg

我迷失了我的 PyQT 程序。我想全屏显示 SVG 图像,但我需要能够设置图像的比例和位置,并且屏幕的其余部分填充黑色。 我在彼此之上使用 QSvgWidget 和常规 QWidget,但这不是一个好的解决方案,因为它作为两个进程和两个单独的窗口运行。 你能告诉我如何只用一个小部件制作这个吗

import sys, os
from PyQt4 import QtGui, QtSvg

class Display(QtSvg.QSvgWidget):
    def __init__(self, parent=None):
        super(Display, self).__init__(parent)

def main():
    app = QtGui.QApplication(sys.argv)
    black = QtGui.QWidget() #setting background with widget
    black.showFullScreen()
    form = Display()
    form.setWindowTitle("Display SVG Layer")

    form.showFullScreen()

    form.setStyleSheet("background-color:black;")
    form.load("E:\example.svg")

    form.move(100,0)
    form.resize(1900,1000)

    app.exec_()

if __name__ == '__main__':
    main()

最佳答案

也许你可以使用QGraphicsViewQGraphicsScene:

class MyGraphicsView(QGraphicsView):
    def __init__(self, w, h, parent=None):
        QGraphicsView.__init__(self, parent)

        self.setGeometry(0, 0, w, h)                # screen size

class MyGraphicsScene(QGraphicsScene):
    def __init__(self, w, h, parent = None):
        QGraphicsScene.__init__(self,parent)

        self.setSceneRect(0, 0, w, h)           # screen size

        self.backgroundPen = QPen(QColor(Qt.black))
        self.backgroundBrush = QBrush(QColor(Qt.black))

        self.textPen = QPen(QColor(Qt.lightGray))
        self.textPen.setWidth(1)
        self.textBrush = QBrush(QColor(Qt.lightGray))
        self.textFont = QFont("Helvetica", 14, )

        # paint the background
        self.addRect(0,0,self.width(), self.height(), self.backgroundPen, self.backgroundBrush)

        # paint the svg-title
        self.svgTitle = self.addSimpleText('Display SVG Layer', self.textFont)
        self.svgTitle.setPen(self.textPen)
        self.svgTitle.setBrush(self.textBrush)
        self.svgTitle.setPos(200,75)

        # paint the svg
        self.svgItem = QGraphicsSvgItem('./example.svg')
        '''
        edit:
        if necessary, get the size of the svgItem to calculate 
        scale factor and position
        '''
        self.svgSize = self.svgItem.renderer().defaultSize()
        self.svgItem.setScale(0.25)                     # scale the svg to an appropriate size
        self.addItem(self.svgItem)
        self.svgItem.setPos(200, 125)

if __name__ == '__main__':
    app = QApplication(sys.argv)

    screen_size = app.primaryScreen().size()        
    width = screen_size.width()
    height = screen_size.height()

    graphicsScene = MyGraphicsScene(width, height)

    graphicsView = MyGraphicsView(width, height)
    graphicsView.setScene(graphicsScene)
    graphicsView.show()
    app.exec_() 

关于svg - 使用 PyQT 中的 QSvgWidget 全屏设置 SVG 图像的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36970524/

相关文章:

javascript - 使用 javascript 更改 svg 内所有元素的填充

javascript - Python CGI 脚本拒绝包含数据 URI 的数据 (403)

python - PyQt 在 for 循环内部连接与单独调用会导致不同的行为

javascript - D3 过渡 : Fading in and out the colors within a gradient fill

css - 如何在没有 View 框的情况下彼此相邻绘制 svg 形状

python - 安装 PyQt5 时出错 : const class QJsonValue’ has no member named ‘toInt’

python - 属性错误 : 'QWheelEvent' object has no attribute 'delta'

c++ - 如何在QT中打开svg文件(QGraphicsView)

c++ - 从 QtCreator 运行 NetAnim 时出错