I'm learning Python and Qt, and as an exercise I designed a simple window in QT Designer with a QGraphicsView which would represent a stack data structure. It should hold the items on the stack as a rectangle with a label representing the item, but my problem is that I can't position the rectangle at(x,y). Google and the documentation didn't help. Maybe it's a simple oversight or it's just me being dumb. This code draws a rectangle in the center of the QGraphicsView, and it should(if I understand correctly)place a 10x10 rectangle at 10,10:
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>
from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class Ui(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('Stack.ui', self)
self.scene = QGraphicsScene()
self.graphicsView.setScene(self.scene)
self.graphicsView.setSceneRect(0, 0, 250, 250) # adjust as needed
self.graphicsView.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.scene.addRect(QRectF(10, 10, 10, 10))
app = QApplication([])
window = Ui()
window.show()
app.exec()