首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PyQT中的QSvgWidget在全屏中设置SVG图像的尺寸?

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

Stack Overflow用户
提问于 2016-05-02 02:43:34
回答 1查看 1.7K关注 0票数 2

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2016-05-03 18:24:45

也许您可以使用QGraphicsViewQGraphicsScene

代码语言:javascript
复制
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_() 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36970524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档