[PyQt5]QGraphicsView入门学习

在进行QGraphicsView学习时,发现很多博客都是使用C++编写代码,其中不乏一些宝藏博客。但是自己是使用python+PyQt5编程,于是将别人的代码用python重新翻译了一遍,仅供参考。

lucky-billy博主写了很多Qt方面的博客,都很不错。于是,我边学边写了下面这篇
Qt之QGraphicsView入门篇

TestGraphicsView.ui设计:一个Dialog窗口,里面添加一个graphicsView控件
在这里插入图片描述
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"))

如下代码是对参考博客中示例代码的Python翻译版:

import sys

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt, QLineF, QPointF, pyqtSignal
from PyQt5.QtGui import QPen, QColor, QBrush, QPainterPath, QPolygonF
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsRectItem, QGraphicsItem, QGraphicsLineItem, QGraphicsPathItem, \
    QGraphicsPolygonItem, QGraphicsView
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_lineItem = QGraphicsLineItem()  # 定义一个直线图元
        m_lineItem.setLine(QLineF(0, 0, 100, 100))
        m_lineItem.setPen(pen)
        m_lineItem.setFlag(QGraphicsItem.ItemIsMovable)

        m_pathItem = QGraphicsPathItem()  # 定义一个路径图元
        path = QPainterPath()
        path.moveTo(90, 50)
        for i in range(1, 6):
            path.lineTo(50 + 40 * math.cos(0.8 * i * math.pi), 50 + 40 * math.sin(0.8 * i * math.pi))
        path.closeSubpath()
        m_pathItem.setPath(path)
        m_pathItem.setPen(pen)
        m_pathItem.setFlag(QGraphicsItem.ItemIsMovable)

        m_polygonItem = QGraphicsPolygonItem()  # 定义一个多边形图元

        polygon = QPolygonF([QtCore.QPointF(-100.0, -150.0), QtCore.QPointF(-120.0, 150.0), QtCore.QPointF(320.0, 160.0), QtCore.QPointF(220.0, -140.0)])
        # polygon[4] = {QPointF(-100.0, -150.0), QPointF(-120.0, 150.0), QPointF(320.0, 160.0), QPointF(220.0, -140.0)}
        m_polygonItem.setPolygon(polygon)
        m_polygonItem.setPen(pen)
        m_polygonItem.setFlag(QGraphicsItem.ItemIsMovable)

        scene.addItem(m_rectItem)  # 把矩形图元添加到场景
        scene.addItem(m_lineItem)  # 把直线图元添加到场景
        scene.addItem(m_pathItem)  # 把路径图元添加到场景
        scene.addItem(m_polygonItem)  # 把多边形图元添加到场景

        # 定义一个视图,并把场景添加到视图

        self.graphicsView.setScene(scene)
        self.graphicsView.resize(800, 600)
        self.graphicsView.show()


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

结果如下:

QGraphicsView入门学习

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值