Issue
I have a QGraphicsRect
that changes its height dynamically. I want to normalize its height to 1 after its height changed. If I try to apply a QTransform
or the scale()
function, the y
position of the QGraphicsRect
changes as well if its y
position is not 0.
I tried to move the QGraphicsRect
back to the origin of the scene before applying the scaling, but it did not help. Any ideas?
Minimal working example below. (Press the button to change the height of the QRectItem
). If the problem is solved, the QGraphicsRect
upper and lower edge should always lay on the red lines.
import sys
import numpy as np
from PySide import QtCore, QtGui
class Test(QtGui.QMainWindow):
def __init__(self):
super(Test, self).__init__()
self.setupUi(self)
self.setupGV()
self.connectElements()
self.rectY = 3
self.initRect()
self.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gv_center = QtGui.QGraphicsView(self.centralwidget)
self.gv_center.setGeometry(QtCore.QRect(100, 60, 561, 331))
self.gv_center.setObjectName("gv_center")
self.pb_debug = QtGui.QPushButton(self.centralwidget)
self.pb_debug.setGeometry(QtCore.QRect(540, 500, 94, 24))
self.pb_debug.setObjectName("pb_debug")
self.pb_debug.setText("push !")
MainWindow.setCentralWidget(self.centralwidget)
def connectElements(self):
self.pb_debug.clicked.connect(self.buttonClick)
def setupGV(self):
self.overviewScene = QtGui.QGraphicsScene(self)
self.overviewScene.setSceneRect(0, -0.5, 1, 7)
self.gv_center.setScene(self.overviewScene)
self.gv_center.fitInView(0, -0.5, 1, 7)
def initRect(self):
self.overviewScene.addLine(-5, self.rectY, 5, self.rectY, QtGui.QPen(QtGui.QColor(255, 0, 0)))
self.overviewScene.addLine(-5, self.rectY + 1, 5, self.rectY + 1, QtGui.QPen(QtGui.QColor(255, 0, 0)))
self.rect = self.overviewScene.addRect(0, self.rectY, 1, 1, QtGui.QPen(QtGui.QColor(0, 0, 0)))
def buttonClick(self):
newHeight = np.random.randint(1, 10)
print(newHeight)
geo = self.rect.rect()
geo.setHeight(newHeight)
self.rect.setRect(geo)
self.normalizeSubplot(self.rect, newHeight, self.rectY)
def normalizeSubplot(self, subplotItem, accH, y):
subplotItem.prepareGeometryChange()
if accH != 0:
height = 1.0 / accH
else:
height = 0
trans = subplotItem.transform()
trans.setMatrix(1, trans.m12(), trans.m13(),
trans.m21(), height, trans.m23(),
trans.m31(), trans.m32(), 1)
subplotItem.setTransform(trans)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Test()
sys.exit(app.exec_())
Solution
After a couple of days I found the solution with help of some guy from IRC. The transformation matrix has to be:
trans.setMatrix(1, trans.m12(), trans.m13(),
trans.m21(), height, trans.m23(),
trans.m31(), -height * self.rectY + self.rectY, 1)
Answered By - P.R.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.