PyQt5 QGraphicsScene应用

QGraphicsScene添加文本

要想将元素添加到场景中,首先你必须构建一个 QGraphicsScene图形场景,然后通过调用addItem()将元素添加到你构建的QGraphicsScene图形场景中。QGraphicsScene图形场景还有很多相当便利的方法,如:addEllipse(), addLine(), addPath(), addPixmap(), addPolygon(), addRect(),addText()等。所有添加的元素都具有他的尺寸和相对于场景的坐标系,元素初始化位置为所在场景中的(0,0)。

然后,使用QGraphicsView图形窗口来加载QGraphicsScene图形场景,就可以看到你在场景中添加的元素啦。当场景变化时,(例如,当一个元素移动或变换)QGraphicsScene图形场景发出的changed()信号。如果要删除场景中的某个元素用removeItem()。

import sys
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QGraphicsScene, QGraphicsView


class sceneDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('SceneDemo')
        self.resize(600, 500)
        self.Label = QLabel()
        self.hbox = QHBoxLayout()
        self.scene = QGraphicsScene()
        self.scene.setSceneRect(0, 0, 300, 300)
        self.scene.addText('PyQt5',font=QFont("Roman times",80,QFont.Bold))
        self.view = QGraphicsView()
        self.view.resize(600,500)
        self.view.setScene(self.scene)
        self.hbox.addWidget(self.view)
        self.hbox.setContentsMargins(0,0,0,0)
        self.Label.setLayout(self.hbox)
        self.setLayout(self.hbox)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = sceneDemo()
    win.show()
    sys.exit(app.exec_())

运行结果
在这里插入图片描述

发布了92 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zZzZzZ__/article/details/103866454
今日推荐