I'm asked to create a GUI with PyQt without using any Qtdesigner for my assignment. But now I'm facing a problem. In this pic GUI screenshoot
as you can see, there is a spinbox "Anzahl der Schicht". What I want to do is, when the user sets the value for this spinbox, the area which below it will show the corresponding rows of the input(the combination of QLineEdit,QSlider Widget,QLineEdit and 2 QSpinboxes in a row).
For example, the pic that I uploaded, means the value of spinbox "Anzahl der Schicht" is 3, so there are 3 rows below it. If the value is 4, there should be 4 rows. There is no limited value for the spinbox. How can I make this kind of dynamic effect for the GUI?
Update on 05.07.2019 Thanks for all useful replies. The following code and pic GUI version2 is my current status. So far I can't connect the Qspinbox to the class Widget() for adding or deleting the rows. So I just use a button "add widget" to implement what I want.
class ExampleWidget(QtWidgets.QGroupBox):
def __init__(self, numAddWidget):
QtWidgets.QGroupBox.__init__(self)
self.numAddWidget = numAddWidget
self.initSubject()
self.organize()
self.setFlat(True)
self.setStyleSheet("border: 1px solid transparent")
def initSubject(self):
self.shiftname =QtWidgets.QLineEdit() # Eingabefeld init
self.shiftname.setText('0')
self.shiftpercent = QtWidgets.QSlider()
self.shiftpercent.setOrientation(QtCore.Qt.Horizontal)
self.carnum =QtWidgets.QLineEdit() # Eingabefeld init
self.carnum.setText('0')
self.start = QtWidgets.QTimeEdit()
self.start.setDisplayFormat("HH:mm")
self.end = QtWidgets.QTimeEdit()
self.end.setDisplayFormat("HH:mm")
def organize(self):
grid = QtWidgets.QGridLayout(self)
self.setLayout(grid)
grid.addWidget(self.shiftname, 0,0)
grid.addWidget(self.shiftpercent, 0,1)
grid.addWidget(self.carnum, 0,2)
grid.addWidget(self.start, 0,3)
grid.addWidget(self.end, 0,4)
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.numAddWidget = 1
self.initUi()
def initUi(self):
self.layoutV = QtWidgets.QVBoxLayout(self)
self.area = QtWidgets.QScrollArea(self)
self.area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.layoutH = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
self.gridLayout = QtWidgets.QGridLayout()
self.layoutH.addLayout(self.gridLayout)
self.area.setWidget(self.scrollAreaWidgetContents)
self.add_button = QtWidgets.QPushButton("Add Widget")
self.layoutV.addWidget(self.add_button)
self.layoutV.addWidget(self.area)
self.add_button.clicked.connect(self.addWidget)
self.widget = ExampleWidget(self.numAddWidget)
self.gridLayout.addWidget(self.widget)
def addWidget(self):
self.numAddWidget += 1
self.widget = ExampleWidget(self.numAddWidget)
self.gridLayout.addWidget(self.widget)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())