[英]How to make dynamic row and columns of qlineedit on a click of pushbutton in pyqt5?
我正在進行一個庫存管理軟件項目,該項目的一小部分是一些qlineedits和qlable的集合,這些內容可以通過單擊按鈕創建,
即默認情況下,將打開一個安排,如下所示
lineedit lineEdit lable pushbutton
在上面的排列中,當我單擊按鈕時,將以與上面相同的排列創建新行,下面用代碼示例顯示了這些行,並將這些lineedit中的數據保存在數組中
lineedit lineEdit lable pushbutton
lineedit lineEdit lable pushbutton
lineedit lineEdit lable pushbutton
我實際上沒有得到確切的方法來執行此操作,我嘗試了不同的方法,使用for循環創建此方法,並使用方法設置創建上述方法,但是所有方法都存在一些問題,任何人都可以提供必要的算法或可以修改的代碼或使用?
如圖所示,我想在產品詳細信息段中實現代碼
碼:
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'
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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.