在 for 循环中使用 scene.addRect。如何在矩形的名称中添加一个变量编号?
Using scene.addRect in for loop. How do I add a variable number to the rectangle's name?
首先我想提一下,我的编程经验和知识非常有限。如果您能用我可以简单地复制和粘贴的实际代码回答我的问题,我将不胜感激。
我正在 Python 2.7 中为 QGIS
编写一个插件,使用 PyQt4
创建 GUI。
我正在使用 for 循环在 QGraphicsScene
中创建成对的矩形。每次迭代都会给出一对矩形。现在我想在 for 循环之外访问这些矩形,但是左侧的所有矩形都称为 rect_L
,右侧的所有矩形都称为 rect_R
。有没有办法在他们的名字中添加一个可变数字?我想调用第一对 rect_L_1
和 rect_R_1
,第二对 rect_L_2
和 rect_R_2
等等(或类似的东西)。
我希望我的问题足够清楚。 link 到 QGraphicsScene
的屏幕截图,如 QGIS
所示,代码的相关部分(我认为)如下。提前致谢。
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))
使用一两个列表。
L_rects = []
R_rects = []
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)
L_rects.append(rect_L)
R_rects.append(rect_R)
count_valid += 1
count_j += 1
count_i += 1
现在您可以使用索引访问单个矩形对象,例如。 L_rects[2]
.
首先我想提一下,我的编程经验和知识非常有限。如果您能用我可以简单地复制和粘贴的实际代码回答我的问题,我将不胜感激。
我正在 Python 2.7 中为 QGIS
编写一个插件,使用 PyQt4
创建 GUI。
我正在使用 for 循环在 QGraphicsScene
中创建成对的矩形。每次迭代都会给出一对矩形。现在我想在 for 循环之外访问这些矩形,但是左侧的所有矩形都称为 rect_L
,右侧的所有矩形都称为 rect_R
。有没有办法在他们的名字中添加一个可变数字?我想调用第一对 rect_L_1
和 rect_R_1
,第二对 rect_L_2
和 rect_R_2
等等(或类似的东西)。
我希望我的问题足够清楚。 link 到 QGraphicsScene
的屏幕截图,如 QGIS
所示,代码的相关部分(我认为)如下。提前致谢。
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))
使用一两个列表。
L_rects = []
R_rects = []
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)
L_rects.append(rect_L)
R_rects.append(rect_R)
count_valid += 1
count_j += 1
count_i += 1
现在您可以使用索引访问单个矩形对象,例如。 L_rects[2]
.