如何在pyqt5中单击按钮即可创建qlineedit的动态行和列?

时间:2019-09-14 20:53:05

标签: python python-3.x pyqt5

我正在开发一个库存管理软件项目,其中我有一小部分是一些qlineedits和qlable的集合,只需单击一个按钮即可创建

即默认情况下,将打开一个安排,如下所示

lineedit   lineEdit   lable  pushbutton

在上述布置中,当我单击按钮时,将以与上面相同的布置创建新行,下面用代码示例显示了这些行,并将这些lineedit中的数据保存在数组中

lineedit   lineEdit   lable  pushbutton
lineedit   lineEdit   lable  pushbutton
lineedit   lineEdit   lable  pushbutton

我实际上并没有确切的方法来做这件事,我尝试了不同的方法,使用for循环创建了这个,并使用方法安排创建了上面的方法,但是所有人都有一些问题,谁能提供一个必要的算法或代码呢?我可以修改或使用?

screenshot

如图所示,我要在产品详细信息段中实现代码

代码:

class Ui_MainWindow(QObject):

    def setupUi2(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(1500, 799)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
............
............
        self.CREATEBILL = QtWidgets.QWidget()
        self.CREATEBILL.setObjectName("CREATEBILL")
        self.gridLayout_7 = QtWidgets.QGridLayout(self.CREATEBILL)
      ............
..................
       # where to implement codes
        self.splitter_15 = QtWidgets.QSplitter(self.scrollAreaWidgetContents_9)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)     
sizePolicy.setHeightForWidth(self.splitter_15.sizePolicy().hasHeightForWidth())

我尝试过但未成功的代码,如@oetzi所教:


class Widget(QWidget):
    form_group_box = None
    layout = None
    row_counter = None

    def __init__(self):
        super(QWidget, self).__init__()
        self.row_counter = 0
        self.create_form_groupbox()
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self._insert)
        button_box.rejected.connect(self._exit)
        button_box.button(QDialogButtonBox.Ok).setText("Insert")
        button_box.button(QDialogButtonBox.Cancel).setText("Cancel")

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.form_group_box)
        main_layout.addWidget(button_box)
        self.setLayout(main_layout)
        self.setWindowTitle("form example")

    def _insert(self):
        self._insert_row_component()
        i = self.row_counter
        print(i)
        a = i - 2
        b = self.qline_edit1[a].text()
        print(b)


    def _exit(self):
        pass

    def _insert_row_component(self):

        i = self.row_counter

        self.qline_edit1 = [None]*i
        self.qline_edit1.append(self.qline_edit1)
        self.qline_edit1[i] = QLineEdit()
        self.qline_edit1[i].setPlaceholderText("enter text1")

        push_button = QPushButton("push button")
        push_button.clicked.connect(self._on_push_button_clicked)

        qline_edit2 = QLineEdit()
        qline_edit2.setPlaceholderText("enter text2")

        qline_edit3 = QLineEdit()
        qline_edit3.setPlaceholderText("enter text3")
        qline_edit4 = QLineEdit()
        qline_edit4.setPlaceholderText("enter text4")
        qlabel = QLabel("label")


        self.layout.addWidget(self.qline_edit1[i], self.row_counter, 0)
        self.layout.addWidget(qline_edit2, self.row_counter, 1)

        self.layout.addWidget(qline_edit3, self.row_counter, 3)
        self.layout.addWidget(qline_edit4, self.row_counter, 2)

        self.layout.addWidget(qlabel, self.row_counter, 4)
        self.layout.addWidget(push_button, self.row_counter, 5)
        self.row_counter = self.row_counter + 1



    def _on_push_button_clicked(self):
        # TODO implement
        print("button clicked")
        a = self.qline_edit1[1].text()
        print(a)

    def create_form_groupbox(self):
        self.form_group_box = QGroupBox("Form layout")
        self.layout = QGridLayout()
        self._insert_row_component()
        self.form_group_box.setLayout(self.layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = Widget()
    widget.show()
    sys.exit(app.exec_())

,但遇到不同的错误: 的

'NoneType' object has no attribute 'text'

1 个答案:

答案 0 :(得分:2)

form new insert

from PyQt5.QtWidgets import (QApplication, QPushButton, QWidget, QDialogButtonBox, QGridLayout, QLabel, QLineEdit,
                                 QVBoxLayout, QGroupBox)

import sys


class Widget(QWidget):
    form_group_box = None
    layout = None
    row_counter = None

    def __init__(self):
        super(QWidget, self).__init__()
        self.row_counter = 0
        self.create_form_groupbox()
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self._insert)
        button_box.rejected.connect(self._exit)
        button_box.button(QDialogButtonBox.Ok).setText("Insert")
        button_box.button(QDialogButtonBox.Cancel).setText("Cancel")

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.form_group_box)
        main_layout.addWidget(button_box)
        self.setLayout(main_layout)
        self.setWindowTitle("form example")

    def _insert(self):
        self._insert_row_component()

    def _exit(self):
        pass

    def _insert_row_component(self):
        push_button = QPushButton("push button")
        push_button.clicked.connect(self._on_push_button_clicked)
        qline_edit1 = QLineEdit()
        qline_edit1.setPlaceholderText("enter text")
        qline_edit2 = QLineEdit()
        qline_edit2.setPlaceholderText("enter text")
        qlabel = QLabel("label")
        self.layout.addWidget(qline_edit1, self.row_counter, 0)
        self.layout.addWidget(qline_edit2, self.row_counter, 1)
        self.layout.addWidget(qlabel, self.row_counter, 2)
        self.layout.addWidget(push_button, self.row_counter, 3)
        self.row_counter = self.row_counter + 1

    def _on_push_button_clicked(self):
        # TODO implement
        print("button clicked")

    def create_form_groupbox(self):
        self.form_group_box = QGroupBox("Form layout")
        self.layout = QGridLayout()
        self._insert_row_component()
        self.form_group_box.setLayout(self.layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = Widget()
    widget.show()
    sys.exit(app.exec_())
相关问题