Search code examples
pythonpyqtqlineedit

PyQt, set specific number of Line Edit inputs


I would like to set number of inputs (input = line edit) in one step and after click on the button I want to get the number of line edits. (As is shown in picture.)

enter image description here

For example, I have 3 input parameters, so I want to set the number 3, click on button and get 3 fields (line edits) for the inputs.

I don't want to create new widget for every option and replace the old one. (I have 4 parameters with variable value of inputs, too many combinations.) Eventually I could insert multiple values into one into line edit but neither of these options sounds good to me. Do you have any tips?

Thank you for your answers.


Solution

  • Try it:

    from PyQt5 import QtWidgets
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, *args, **kwargs):
            super().__init__()
            self.resize(300,300)
            self.items = []
            self.item_count = 0
    
            label = QtWidgets.QLabel("NUMBER OF LINE EDITS")
    
            self.spinBox = QtWidgets.QSpinBox(self)
            self.spinBox.setRange(0, 7)
            self.spinBox.valueChanged.connect(self.set_item_count)
    
            button = QtWidgets.QPushButton("apply", clicked=self.on_clicked)
    
            self.lineEdit = QtWidgets.QLineEdit
    
            groupBox = QtWidgets.QGroupBox("Line Edit")
            self.item_layout = QtWidgets.QVBoxLayout(groupBox)
            self.item_layout.addStretch(2)
    
            g_layout = QtWidgets.QGridLayout(self)
            g_layout.addWidget(label, 0, 0, 1, 2)
            g_layout.addWidget(self.spinBox, 0, 2, 1, 1)
            g_layout.addWidget(button, 1, 0, 1, 1)
            g_layout.addWidget(groupBox, 2, 0, 5, 3)
    
        def on_clicked(self):
            print( *[ item.text() for item in  self.items[:self.spinBox.value()] ], sep="\n" )
    
        def set_item_count(self, new_count:int):
            n_items = len(self.items)
            for ii in range(n_items, new_count):
                item = self.lineEdit(self)
                self.items.append(item)
                self.item_layout.insertWidget(n_items, item)
            for ii in range(self.item_count, new_count):
                self.item_layout.itemAt(ii).widget().show()
            for ii in range(new_count, self.item_count):
                self.item_layout.itemAt(ii).widget().hide()
            self.item_count = new_count
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication([])
        window = Widget()
        window.show()
        app.exec()
    

    enter image description here