如何从 qlineedit 获取所有文本
How to get all text from qlineedit
我只想知道如何在单击 'Execute the values' 按钮后存储所有 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 引用被销毁。所以你必须注意一旦它们被摧毁就不要使用它们。
我只想知道如何在单击 'Execute the values' 按钮后存储所有 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 引用被销毁。所以你必须注意一旦它们被摧毁就不要使用它们。