Pyqt4 在创建动态表单时访问对象

Pyqt4 accessing objects when creating a dynamic form

我创建了一个布局,一旦我将其分配到列表中,它就会自动添加标签和行编辑,但是对于我来说,要创建一个用户,我需要行编辑和组合框中的值,但正如你所看到的,我已经设置了每个对象名称,我如何访问我的 tryCreateUser() 函数中的所有值?如您所见,我通过打印编辑进行了检查,但这只给了我最后一个对象!!提前感谢您的帮助!

def addNewUser(self):
    def tryCreateUser():
        print(edit)

    self.deleteLayout(self.dynamicFrame.layout())

    grid = QtGui.QGridLayout()
    grid.setSpacing(10)
    row = 1
    column = 1
    edit = ['First Name', 1, 'Last Name', 1, 'Date of Birth', 2, '', 'Gender', 3, 'Access Level', 4, 'Password', 5, 'Verify Password', 5]

    for item in edit:
        if item == '':
            self.dob = QtGui.QLabel()
            grid.addWidget(self.dob, 3, 3)
            column -= 1         
        else:
            if item == 1:
                edit = QtGui.QLineEdit()
            elif item == 2:
                edit = QtGui.QPushButton('Choose')
                edit.clicked.connect(self.openCal)
            elif item == 3:
                edit = QtGui.QComboBox()
                edit.addItem('Male')
                edit.addItem('Female')
            elif item == 4:
                edit = QtGui.QComboBox()
                edit.addItem('General Staff')
                edit.addItem('Stock Admin')
                edit.addItem('Manager')
            elif item == 5:
                edit = QtGui.QLineEdit()
                edit.setEchoMode(QtGui.QLineEdit.Password)
            else:
                edit = QtGui.QLabel(item)

        edit.setObjectName(str(item))
        grid.addWidget(edit, row, column)
        if column >= 2:
            column = 1
            row += 1
        else:
            column += 1

    createButton = QtGui.QPushButton("Create User")
    createButton.clicked.connect(tryCreateUser)
    cancelButton = QtGui.QPushButton("Cancel")
    cancelButton.clicked.connect(self.populateUser)
    grid.addWidget(createButton, row , column)
    grid.addWidget(cancelButton, row, (column+1))

    self.dynamicFrame.setLayout(grid)

你应该让每个按钮在某个地方的字典中注册自己。试试这个:

from collections import namedtuple

Button = namedtuple('Button', ["title", "to_do"])

buttons_to_make = [Button("First Name", [1]),
                   Button("Last Name", [1]),
                   Button("Date of Birth", [2, '']),
                   ...]

self.buttons = {}
for button in buttons_to_make:
    for action in button.to_do:
        # your big long if/elif structure here, e.g.:
        if action == 1:
            b = QtGui.QLineEdit()
        elif action == 2:
            b = QtGui.QPushButton('Choose')
            b.clicked.connect(self.openCal)
        # etc
    self.buttons[button.title] = b

然后您在 buttons 中拥有所有内容的中央存储库,并且可以对其进行迭代。

def tryCreateUser():
    for title, button in self.buttons.items():
        print("Title is {}, button obj is {}".format(title, button))

请注意,这与我的做法不完全相同,但它最接近您的原始示例。老实说,每个 if 块都应该是它自己的函数,这样你就会有类似的东西:

def __create_QLineEdit(self):
    b = QtGui.QLineEdit()
    return b

def __create_QPushButton(self):
    b = QtGui.QPushButton("Choose")
    b.clicked.connect(self.openCal)
    self.dob = QtGui.QLabel()
    grid.addWidget(self.dob, 3, 3)
    column -= 1
    return b

...

然后您将 namedtuple 定义为:

Button = namedtuple("Button", ['name','action'])

并将buttons_to_make定义为

buttons_to_make = [Button("First Name", self.__create_QLineEdit),
                   Button("Last Name", self.__create_QLineEdit),
                   Button("Date of Birth", self.__create_QPushButton),
                   ...]

并执行:

buttons = {}
for button in buttons_to_make:
    name, action = button
    buttons[name] = action()