[PyQt5]QGraphicsView进阶学习

参考博客:Qt之QGraphicsView进阶篇
python代码翻译如下:

TestGraphicsView.ui设计:一个Dialog窗口,里面添加一个graphicsView控件
[PyQt5]QGraphicsView进阶学习_第1张图片
TestGraphicsView.ui自动生成的TestGraphicsView.py代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'TestGraphicsView.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(678, 521)
        self.graphicsView = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView.setGeometry(QtCore.QRect(70, 70, 256, 192))
        self.graphicsView.setObjectName("graphicsView")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

参考博客的实例代码翻译:

testQGraphicsView_development.py为主函数界面

import sys

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QLineF, pyqtSignal, QObject
from PyQt5.QtGui import QPen, QColor, QBrush, QPainterPath, QPolygonF
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsRectItem, QGraphicsItem, QGraphicsLineItem, QGraphicsPathItem, \
    QGraphicsPolygonItem
import math
from PyQt5.QtWidgets import *

from Test.TestGraphicsView import Ui_Dialog


class TestForm(QtWidgets.QDialog, Ui_Dialog):
    tableWidgetMessage = pyqtSignal(object)

    def __init__(self, parent=None):
        super(TestForm, self).__init__(parent)
        self.setupUi(self)
        self.initParam()

    def initParam(self):
        self.resize(900, 800)

        scene = QGraphicsScene()  # 定义一个场景,设置背景色为红色
        scene.setBackgroundBrush(Qt.red)

        pen = QPen()  # 定义一个画笔,设置画笔颜色和宽度
        pen.setColor(QColor(0, 160, 230))
        pen.setWidth(10)

        m_rectItem = QGraphicsRectItem()  # 定义一个矩形图元
        m_rectItem.setRect(0, 0, 80, 80)
        m_rectItem.setPen(pen)
        m_rectItem.setBrush(QBrush(QColor(255, 0, 255)))
        m_rectItem.setFlag(QGraphicsItem.ItemIsMovable)
        m_rectItem.moveBy(600,300)
        scene.addItem(m_rectItem)  # 把矩形图元添加到场景

        dialog = QDialog(None, Qt.CustomizeWindowHint | Qt.WindowTitleHint)
        dialog.setWindowOpacity(0.8)
        dialog.setWindowTitle("operation")
        dialog.setLayout(QVBoxLayout())

        m_zoomInBtn = QPushButton("zoomIn")
        m_zoomOutBtn = QPushButton("zoomOut")
        m_rotateLeftBtn = QPushButton("rotateLeft")
        m_rotateRightBtn = QPushButton("rotateRight")
        dialog.layout().addWidget(m_zoomInBtn)
        dialog.layout().addWidget(m_zoomOutBtn)
        dialog.layout().addWidget(m_rotateLeftBtn)
        dialog.layout().addWidget(m_rotateRightBtn)
        scene.addWidget(dialog)

        # 定义一个视图,并把场景添加到视图
        from Test.MyGraphicsView import MyGraphicsView
        self.graphicsView = MyGraphicsView(self)
        self.graphicsView.setScene(scene)
        self.graphicsView.resize(800, 600)
        self.graphicsView.show()

        # 点击按钮实现视图的缩放和旋转
        m_zoomInBtn.clicked.connect(self.graphicsView.zoomIn)
        m_zoomOutBtn.clicked.connect(self.graphicsView.zoomOut)
        m_rotateLeftBtn.clicked.connect(self.graphicsView.rotateLeft)
        m_rotateRightBtn.clicked.connect(self.graphicsView.rotateRight)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    Form = TestForm()
    Form.show()
    sys.exit(app.exec_())

自定义的MyGraphicsView.py

from PyQt5.QtWidgets import QGraphicsView


class MyGraphicsView(QGraphicsView):

    def __init__(self, parent=None):
        QGraphicsView.__init__(self, parent)

    def zoomIn(self):
        # 放大和缩小其实就是调用QGraphicsview的scale函数, 放大倍数为参数,这个函数涉及的是坐标系的转换。
        QGraphicsView.scale(self, 1.2, 1.2)

    def zoomOut(self):
        QGraphicsView.scale(self, 1 / 1.2, 1 / 1.2)

    def rotateLeft(self):
        QGraphicsView.rotate(self, -30)

    def rotateRight(self):
        QGraphicsView.rotate(self, 30)

运行结果如下:

QGraphicsView进阶学习

你可能感兴趣的:(PyQt5,python,pyqt5)