如何将QLineEdit子类化为索引

时间:2016-04-30 23:48:06

标签: pyqt qlineedit

我需要子类化一个PyQt5 QLineEdit,以便它有一个索引,以便我可以通过以下方式访问它:

    stringvariable = LineEdit(0).text()
    stringvariable = LineEdit(1).text()
    stringvariable = LineEdit(2).text()

这可能吗?

1 个答案:

答案 0 :(得分:0)

为什么不列出您的LineEdits?

mylist = [QLineEdit(), QLineEdit(), QLineEdit()]

index=0
string = mylist[index].text()
print(string)

或者,如果你真的需要来创建一个子类

class MyLineEdit(QLineEdit):
    all_instances = {}
    def __init__(self, index, *args, **kwargs):
        super(MyLineEdit, self).__init__(*args, **kwargs) #call to superclass
        MyLineEdit.all_instances[index] = self #add this instance to MyLineEdit.all_instances

def LineEdit(index):
    return MyLineEdit.all_instances[index]

要使用它,您只需执行以下操作:

#make an instance
edit = MyLineEdit(123)

string = LineEdit(123).text()
#which is the same as:
string = edit.text()