在for循环中使用scene.addRect。如何在矩形名称中添加变量号?

艾尼乌斯(Ienius)

首先,我想提一下,我的编程经验和知识非常有限。如果您能用我可以简单地复制和粘贴的实际代码回答我的问题,我将不胜感激。

我正在QGIS使用Python 2.7编写用于PyQt4创建GUI的插件
我正在使用for循环在中创建成对的矩形QGraphicsScene每次迭代都会给出一对矩形。现在,我想在for循环之外访问这些矩形,但是左侧的所有矩形都被调用rect_L,右侧的所有矩形都被调用rect_R有没有一种方法可以在其名称中添加一个可变数字?我想打电话给第一对rect_L_1rect_R_1,第二对rect_L_2rect_R_2等(或类似的规定)。
我希望我的问题足够清楚。链接到的截图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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章