如何在 PyQt GraphicsView 中在 (x,y) 处绘制矩形?

问题描述 投票:0回答:1

我正在学习 Python 和 Qt,作为练习,我在 QT Designer 中设计了一个简单的窗口,其中带有 QGraphicsView,它代表堆栈数据结构。它应该将堆栈上的项目保存为一个矩形,并带有代表该项目的标签,但我的问题是我无法将矩形定位在(x,y)处。谷歌和文档没有帮助。 也许这只是一个简单的疏忽,或者只是我愚蠢。 这段代码在 QGraphicsView 的中心绘制一个矩形,它应该(如果我理解正确的话)在 10,10:

处放置一个 10x10 矩形
class Ui(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi('Stack.ui', self) 
        
        self.scene = QGraphicsScene()
        self.graphicsView.setScene(self.scene)
    
        self.scene.addRect(QRectF(10,10,10,10))
        
app=QApplication([])

window=Ui()
window.show()
app.exec()

Stack.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>800</width>
   <height>600</height>
  </rect>
 </property>
 <property name="windowTitle">
  <string>MainWindow</string>
 </property>
 <widget class="QWidget" name="centralwidget">
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="1">
    <widget class="QLineEdit" name="lineEdit">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
    </widget>
   </item>
   <item row="0" column="0" rowspan="3">
    <widget class="QGraphicsView" name="graphicsView"/>
   </item>
   <item row="0" column="3">
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>Push</string>
     </property>
    </widget>
   </item>
   <item row="1" column="3">
    <widget class="QPushButton" name="pushButton_2">
     <property name="text">
      <string>Pop</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <widget class="QMenuBar" name="menubar">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>24</height>
   </rect>
  </property>
 </widget>
 <widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
python qt user-interface graphics pyqt5
1个回答
0
投票

创建 QGraphicsScene:这是添加图形项目(如矩形)的位置。

向场景添加一个矩形项目:您可以使用 QGraphicsRectItem 来实现此目的。

将场景设置为QGraphicsView:该视图负责显示场景及其项目。

以下是如何在 (x, y) 处绘制矩形的示例:

导入系统 从 PyQt5.QtWidgets 导入 QApplication、QGraphicsView、QGraphicsScene、QGraphicsRectItem 从 PyQt5.QtCore 导入 QRectF

类 MyGraphicsView(QGraphicsView): def init(自身): 超级()。init()

    # Create a QGraphicsScene
    self.scene = QGraphicsScene()
    self.setScene(self.scene)

    # Define the x, y position and the width, height of the rectangle
    x, y = 50, 50
    width, height = 100, 100

    # Create a QGraphicsRectItem (rectangle) and set its position and size
    rect_item = QGraphicsRectItem(QRectF(x, y, width, height))

    # Add the rectangle item to the scene
    self.scene.addItem(rect_item)

if name == 'main': 应用程序 = QApplication(sys.argv)

# Create a window with the custom QGraphicsView
view = MyGraphicsView()
view.setWindowTitle("PyQt GraphicsView Rectangle Example")
view.setGeometry(100, 100, 400, 300)
view.show()

sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.