【问题标题】:drawing a rectangle in pyqtgraph在pyqtgraph中绘制一个矩形
【发布时间】:2020-05-17 14:32:45
【问题描述】:

我正在尝试在 pyqtgraph 中绘制矩形以显示二维数组中的数据,它位于设计器中创建的窗口内。有没有办法绘制一个矩形并将对象保存到二维数组,以便我以后可以更新它的颜色?我尝试按照自定义绘图示例进行操作,但我不断收到以下错误:

AttributeError: 'QRectF' object has no attribute 'zValue'
def Draw2DSquare(self):
    self.picture = QtGui.QPicture()
    p = QtGui.QPainter(self.picture)
    p.setPen(pg.mkPen('w'))
    p.drawLine(QtCore.QPointF(0, 0), QtCore.QPointF(1, 1))
    p.setBrush(pg.mkBrush('g'))
    p.drawRect(QtCore.QRectF(0, 0, 4.5, 4.5))
    p.end()
    self.graphWidget_2D.addItem(QtCore.QRectF(self.picture.boundingRect()))

我不知道这是否是最好的方法。有没有更简单的方法来画一个矩形?

这是我的 python 文件:

import time
from PyQt5 import QtWidgets, uic, QtGui, QtCore
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import sys
import numpy as np
from PyQt5.QtGui import QIcon, QKeySequence
from PyQt5.QtWidgets import QAction

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        # Load the UI Page
        uic.loadUi('help.ui', self)
        self.showMaximized()
        self.Draw2DSquare()

    def Draw2DSquare(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        p.drawLine(QtCore.QPointF(0, 0), QtCore.QPointF(1, 1))
        p.setBrush(pg.mkBrush('g'))
        p.drawRect(QtCore.QRectF(0, 0, 4.5, 4.5))
        p.end()
        self.graphWidget_2D.addItem(QtCore.QRectF(self.picture.boundingRect()))

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

这是我的 ui 文件,名为 help.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1120</width>
    <height>833</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <widget class="PlotWidget" name="graphWidget_2D" native="true"/>
    </item>
    <item row="0" column="1">
     <widget class="GLViewWidget" name="graphWidget_3D" native="true"/>
    </item>
    <item row="1" column="0">
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>40</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="1">
     <spacer name="horizontalSpacer_2">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>40</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1120</width>
     <height>21</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <addaction name="actionFile"/>
    <addaction name="actionOpen"/>
    <addaction name="actionSave"/>
    <addaction name="separator"/>
    <addaction name="actionOptions"/>
    <addaction name="separator"/>
    <addaction name="actionExit"/>
   </widget>
   <addaction name="menuFile"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="actionOpen">
   <property name="text">
    <string>Open</string>
   </property>
  </action>
  <action name="actionFile">
   <property name="text">
    <string>File</string>
   </property>
  </action>
  <action name="actionOptions">
   <property name="text">
    <string>Options</string>
   </property>
  </action>
  <action name="actionSave">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph.h</header>
   <container>1</container>
  </customwidget>
  <customwidget>
   <class>GLViewWidget</class>
   <extends>QWidget</extends>
   <header>pyqtgraph.opengl.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

【问题讨论】:

  • 哎呀抱歉。我只是添加了代码来满足要求。

标签: python pyqtgraph


【解决方案1】:

addItem 方法需要一个图形项,正如the docs 指出的那样:

addItem(item, *args, **kargs)

向视图框添加一个图形项。如果项目有绘图数据 (PlotDataItem, PlotCurveItem, ScatterPlotItem),它可能包含在 由 PlotItem 执行的分析。

(强调我的)

但是您传递的不是一个 QRectF。您可以使用 QGraphicsRectItem 但场景的坐标与视图框的坐标不匹配,因此您必须基于 GraphicsObject 实现自定义图形项(作为我采用 the official example 的基础):

import sys

from PyQt5 import QtCore, QtGui, QtWidgets, uic

import pyqtgraph as pg


class RectItem(pg.GraphicsObject):
    def __init__(self, rect, parent=None):
        super().__init__(parent)
        self._rect = rect
        self.picture = QtGui.QPicture()
        self._generate_picture()

    @property
    def rect(self):
        return self._rect

    def _generate_picture(self):
        painter = QtGui.QPainter(self.picture)
        painter.setPen(pg.mkPen("w"))
        painter.setBrush(pg.mkBrush("g"))
        painter.drawRect(self.rect)
        painter.end()

    def paint(self, painter, option, widget=None):
        painter.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        # Load the UI Page
        uic.loadUi("help.ui", self)
        self.showMaximized()
        self.draw_2d_square()

    def draw_2d_square(self):
        rect_item = RectItem(QtCore.QRectF(0, 0, 4.5, 4.5))
        self.graphWidget_2D.addItem(rect_item)


def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

【讨论】:

  • 谢谢,只是一个简单的问题。如何将 rect_item 保存到 num 数组?因为我尝试使用 a= np.ones((5,5,5)) 创建一个数组并将 rect_item 保存到 a[0][0][0] 但它引发类型错误:float() 参数必须是字符串或数字,而不是“RectItem”。谢谢
  • @user169808 rect_item 不是数据或数字集合,而是允许您绘制矩形元素的对象,我不明白您对转换为 numpy 的期望
  • 我只想将它保存到一个数组中。使用 numpy 更方便。但我只会创建一个原生 python 数组
  • @user169808 正如我所指出的:您不能将项目保存在 numpy 中,您可以将 QRect 保存在数组中,因为它是一个可用于创建新项目的数值如果你想恢复它。
猜你喜欢
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多