如何从qlineedit获取所有文本

问题描述

我只是想知道如何在单击“执行值”按钮后存储所有qline编辑文本中的所有值,到目前为止,仅存储最近使用的值, 如何遍历所有输入并采用所有单个变量:

def __init__(self):
    super().__init__()
    self.title = 'Sample Dynamic LineEdit'
    self.left = 150
    self.top = 150
    self.width = 400
    self.height = 500
    self.i = 40
    self.j = 80
    self.counter = 1
    self.initUI()

def initUI(self):
    self.setwindowTitle(self.title)
    self.setGeometry(self.left,self.top,self.width,self.height)

    # Create textBox
    # self.textBox = QLineEdit(self)
    # self.textBox.move(20,20)
    # self.textBox.resize(280,40)

    # Create a button in the window
    self.button = QPushButton('Add Line Edit',self)

    # connect button to function on_click
    self.button.clicked.connect(self.on_click)
    self.show()
    pybutton = QPushButton('Execute the entered values',self)
    pybutton.clicked.connect(self.text_click)
    pybutton.resize(160,35)
    pybutton.move(150,0)
    pybutton.show()

@pyqtSlot()
def on_click(self):
    # this creates a new field and label everytime the button is clicked
    self.textBox = QLineEdit(self)
    self.textBox2 = QLineEdit(self)

    self.label = QLabel(self)
    self.label.setText(str(self.counter))
    self.label.move(5,self.i)
    self.button.move(20,self.j)
    self.textBox.move(20,self.i)
    self.textBox.resize(160,40)
    self.textBox2.move(250,self.i)
    self.textBox2.resize(160,40)

    # dynamic object names
    self.textBox.setobjectName("text" + str(self.counter))
    self.textBox.show()
    self.textBox2.show()
    self.label.show()
    self.i += 40
    self.j += 40
    self.counter += 1
    print(self.textBox.objectName())

def text_click(self):
    first = self.textBox.text()
    
    print('Your name: ' + first)
    second = self.textBox2.text()
    print('Your name: ' + second)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

解决方法

您可以为每个QLineEdit命名,然后使用findChild来访问它们:

    ...
    # dynamic object names
    self.textbox.setObjectName("text_" + str(self.counter))
    self.textbox2.setObjectName("text2_" + str(self.counter))
    ...

def text_click(self):
    for i in range(1,self.counter):
        first = self.findChild(QLineEdit,"text_" + str(i)).text()

        print('Your name: ' + first)
        second = self.findChild(QLineEdit,"text2_" + str(i)).text()
        print('Your name: ' + second)

但是恕我直言,将QLineEdit对象添加到纯列表中会更简单:

...
    self.counter = 1
    self.textboxes = []
    self.textboxes2 = []
    self.initUI()
...
@Slot()
def on_click(self):
    # this creates a new field and label everytime the button is clicked
    textbox = QLineEdit(self)
    textbox2 = QLineEdit(self)
    self.textboxes.append(textbox)
    self.textboxes2.append(textbox2)

    label = QLabel(self)
    label.setText(str(self.counter))
    label.move(5,self.i)
    self.button.move(20,self.j)
    textbox.move(20,self.i)
    textbox.resize(160,40)
    textbox2.move(250,self.i)
    textbox2.resize(160,40)

    # dynamic object names
    textbox.setObjectName("text_" + str(self.counter))
    textbox2.setObjectName("text2_" + str(self.counter))
    textbox.show()
    textbox2.show()
    label.show()
    self.i += 40
    self.j += 40
    self.counter += 1
    print(textbox.objectName())

def text_click(self):
    for i in range(self.counter - 1):
        first = self.textboxes[i].text()

        print('Your name: ' + first)
        second = self.textboxes2[i].text()
        print('Your name: ' + second)
...

在Python中,将对象存储在列表中很便宜,因为列表仅包含对该对象的引用。唯一的缺点是Qt对象是内部C ++对象,可以独立于其Python参考进行销毁。因此,一旦它们被销毁,您就必须避免使用它们。