First I'd like to mention that my programming experience and knowledge are very limited. I'd appreciate it if you could answer my question with actual code that I can simply copy and paste.
I am writing a plugin for QGIS
in Python 2.7 using PyQt4
to create the GUI.
I'm using a for loop to create paired rectangles in a QGraphicsScene
. Every iteration gives one pair of rectangles. Now I want to access these rectangles outside of the for loop, but all the rectangles on the left side are called rect_L
and all on the right are called rect_R
. Is there a way to add a variable number to their names? I want to call the first pair rect_L_1
and rect_R_1
, the second pair rect_L_2
and rect_R_2
and so on (or something along those lines).
I hope my question is clear enough. A link to a screenshot of the QGraphicsScene
as seen in QGIS
and the relevant parts of the code (I think) are below. Thanks in advance.
def setupUi(self, Form):
L = ["Forest", "Lake", "Desert"]
changeMatrix = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
count_i = 1
#Form
Form.setObjectName(_fromUtf8("Form"))
Form.resize(600, 600)
#Graphics View
self.graphicsView = QtGui.QGraphicsView(Form)
self.graphicsView.setGeometry(QtCore.QRect(50, 50, 400, 400))
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
#Pen, Brush, Scene
pen = QtGui.QPen(QtGui.QColor(0, 0, 0), 3, QtCore.Qt.SolidLine)
brush = QtGui.QBrush(QtGui.QColor(184, 36, 238))
scene = QtGui.QGraphicsScene()
#Boxes & Labels
count_valid = 0
for i in changeMatrix:
count_j = 1
for j in i:
if j <> 0 and count_i <> count_j:
textitem_L = scene.addText(L[count_j-1])
textitem_R = scene.addText(L[count_i-1])
textitem_L.setPos(0, count_valid*50)
textitem_R.setPos(300, count_valid*50)
rect_L = scene.addRect(50, count_valid*50, 25, 25, pen, brush)
rect_R = scene.addRect(250, count_valid*50, 25, 25, pen, brush)
rect_L.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsFocusable | QtGui.QGraphicsItem.ItemSendsGeometryChanges)
rect_R.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsFocusable | QtGui.QGraphicsItem.ItemSendsGeometryChanges)
rect_L.setAcceptsHoverEvents(True)
rect_R.setAcceptsHoverEvents(True)
count_valid += 1
count_j += 1
count_i += 1
self.graphicsView.setScene(scene)
#Form
Form.setWindowTitle(_translate("Form", "MoveRect", None))