繁体   English   中英

如何动态读取 QLineEdit 数据

[英]How to Read QLineEdit data dynamically

我正在尝试在 PyQt5 中动态创建标签和文本框,但是,我不知道如何在用户点击保存按钮时读取文本框中输入的数据。 我的代码如下所示:

       self.setWindowTitle("Yaml --> Json")
       self.setGeometry(self.left, self.top, self.width, self.height)

       self.createLayout()
       vbox = QVBoxLayout()
       for i in range(0, len(self.listItems)):
           vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
           vbox.addWidget(QLineEdit())
       vbox.addWidget(self.groupBox)
       self.setLayout(vbox)
       self.show()

   def createLayout(self):
       self.groupBox = QGroupBox()
       hboxLayout = QHBoxLayout()

       button = QPushButton("Save", self)
       button.setIcon(QtGui.QIcon("save.png"))
       button.setIconSize(QtCore.QSize(40, 40))
       button.setMinimumHeight(40)
       button.clicked.connect(self.ClickSave)
       hboxLayout.addWidget(button)

       button1 = QPushButton("Exit", self)
       button1.setIcon(QtGui.QIcon("exit.png"))
       button1.setIconSize(QtCore.QSize(40, 40))
       button1.setMinimumHeight(40)
       button1.clicked.connect(self.ClickExit)
       hboxLayout.addWidget(button1)

       self.groupBox.setLayout(hboxLayout)

   def ClickExit(self):
       print("Exited!!")
       sys.exit()

   def ClickSave(self):
       print("Saved!")```

您可以将稍后要访问的小部件分配给实例变量或将它们存储在列表中,例如

    self.line_edit_list = []
    for i in range(0, len(self.listItems)):
        vbox.addWidget(QLabel(list(self.listItems.keys())[i]))
        line_edit = QLineEdit()
        vbox.addWidget(line_edit)
        self.line_edit_list.append(line_edit)

    ....

    def ClickSave(self):
        for edit in self.line_edit_list:
            print(edit.text())

暂无
暂无

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

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