从重现对象名称的列表中调用对象

Call objects from a list which reproduces object names

我正在创建一个有很多复选框的应用程序。每 4 个复选框位于 groupBox_n (n=36) 内,然后这些 groupBox 位于另一个 groupBox 内。

每个复选框都是按照一定的规则命名的,这对我来说很方便。我想访问他们每个人,但我不想每次都输入他们的名字。所以我想在列表中复制他们的名字,这样我就可以遍历列表并根据他们的名字进行控制。

但是当我尝试连接一个从我的列表中调用字符串的按钮时,我无法做到这一点。在这里我用 QLineEdit 复制了一个例子。

是否可以这样做?

调用 findChildren 对我没有帮助,因为那时我不知道我的复选框在我的应用程序中的位置,或者 "who is who" 在那里。用ObjectName调用也不行吧?

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))

        self.My_lineEdit = QtGui.QLineEdit(Form)
        self.My_lineEdit.setObjectName(_fromUtf8("My_lineEdit"))

        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)

        MyStrinng = 'My_lineEdit'
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.Mystring.clear)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

像这样,PyQt 无法将我的字符串识别为对象的名称。我还尝试用 QCore.QString('My_lineEdit') 制作一个 PyQt 字符串,但是 QString 不适用于我的版本 (you can see here)

有了这个我可以重现我所有复选框的名称。

names = []

for x in range(0, 6):
    for y in range(0, 6):
        for z in range(1, 5):
            Names = 's'+str(x)+str(y)+'0'+str(z)
            names.append(Names)
print(names)

如果你想通过对象查找对象,最简单的选择是使用 findChild():

T QObject::findChild(const QString &name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const

Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. Omitting the name argument causes all object names to be matched. The search is performed recursively, unless options specifies the option FindDirectChildrenOnly.

If there is more than one child matching the search, the most direct ancestor is returned. If there are several direct ancestors, it is undefined which one will be returned. In that case, findChildren() should be used.

This example returns a child QPushButton of parentWidget named "button1", even if the button isn't a direct child of the parent:

QPushButton *button = parentWidget->findChild<QPushButton *>("button1");

文档中的 C++ 示例将转换为以下形式的 python:

button = parentWidget.findChild(QPushButton, "button1")

你的情况:

class Ui_Form(object):
    def setupUi(self, Form):
        ...
        # change self.lineEdit to self.My_lineEdit
        #Form is the parent of My_lineEdit                     
        self.My_lineEdit = QtGui.QLineEdit(Form) 
        self.My_lineEdit.setObjectName(_fromUtf8("My_lineEdit"))

        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)

        ...

        le = Form.findChild(QtGui.QLineEdit, "My_lineEdit")
        self.pushButton.clicked.connect(le.clear)