繁体   English   中英

如何从 qlineedit 获取所有文本

[英]How to get all text from 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 中,将 object 存储在列表中的成本很低,因为该列表将仅包含对 object 的引用。这里唯一的缺点是 Qt 对象在内部是 C++ 对象,可以独立于其 8825744285990 对其进行销毁。 因此,一旦它们被销毁,您必须注意不要使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM