如何通过点击按钮复制整行小部件,然后获得新的小部件?

问题描述 投票:0回答:1

假设我们有一个对话框,里面有一排参数供用户修改。

Dialog

这些参数之间有一些联系。用户可以通过 "添加类型 "按钮添加一行,次数不限。我们的目的是复制整个行的widgets,点击按钮,保存widgets之间的连接,并有机会访问这些参数(在这个例子中 - 打印一个文本的第2行Edit包括那些在解释器中的新行。

请帮助我的函数add_row

import sys
from PyQt5 import QtWidgets
from dialog_1 import Ui_Form


class Dialog(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.ui.comboBox.addItems(['123','456','qwerty'])
        self.ui.comboBox.activated.connect(lambda: self.ui.lineEdit1.setText(self.ui.comboBox.currentText()))
        self.ui.lineEdit2.returnPressed.connect(lambda: print(self.ui.lineEdit2.text()))

        self.ui.pushButton.clicked.connect(self.add_row)

    def add_row(self):
        pass



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = Dialog()
    main.show()
    sys.exit(app.exec_())

一个 dialog_1.py 文件。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
  def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(774, 169)
    self.frame = QtWidgets.QFrame(Form)
    self.frame.setGeometry(QtCore.QRect(0, 0, 661, 61))
    sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(100)
    sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
    self.frame.setSizePolicy(sizePolicy)
    self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
    self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
    self.frame.setObjectName("frame")
    self.gridLayout_5 = QtWidgets.QGridLayout(self.frame)
    self.gridLayout_5.setObjectName("gridLayout_5")
    self.pushButton2 = QtWidgets.QPushButton(self.frame)
    self.pushButton2.setObjectName("pushButton2")
    self.gridLayout_5.addWidget(self.pushButton2, 0, 4, 1, 1)
    self.label = QtWidgets.QLabel(self.frame)
    self.label.setObjectName("label")
    self.gridLayout_5.addWidget(self.label, 0, 0, 1, 1)
    self.comboBox = QtWidgets.QComboBox(self.frame)
    self.comboBox.setObjectName("comboBox")
    self.gridLayout_5.addWidget(self.comboBox, 0, 5, 1, 1)
    self.checkBox = QtWidgets.QCheckBox(self.frame)
    sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.checkBox.sizePolicy().hasHeightForWidth())
    self.checkBox.setSizePolicy(sizePolicy)
    self.checkBox.setLayoutDirection(QtCore.Qt.LeftToRight)
    self.checkBox.setText("")
    self.checkBox.setAutoRepeat(False)
    self.checkBox.setObjectName("checkBox")
    self.gridLayout_5.addWidget(self.checkBox, 0, 6, 1, 1)
    self.lineEdit1 = QtWidgets.QLineEdit(self.frame)
    self.lineEdit1.setObjectName("lineEdit1")
    self.gridLayout_5.addWidget(self.lineEdit1, 0, 1, 1, 1)
    self.lineEdit2 = QtWidgets.QLineEdit(self.frame)
    self.lineEdit2.setObjectName("lineEdit2")
    self.gridLayout_5.addWidget(self.lineEdit2, 0, 8, 1, 1)
    self.pushButton1 = QtWidgets.QPushButton(self.frame)
    self.pushButton1.setObjectName("pushButton1")
    self.gridLayout_5.addWidget(self.pushButton1, 0, 9, 1, 1)
    self.pushButton = QtWidgets.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(680, 20, 75, 23))
    self.pushButton.setObjectName("pushButton")

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

  def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.pushButton2.setText(_translate("Form", "..."))
    self.label.setText(_translate("Form", "#1"))
    self.pushButton1.setText(_translate("Form", "..."))
    self.pushButton.setText(_translate("Form", "Add Type"))enter code here
python python-3.x pyqt pyqt5 qt-designer
1个回答
0
投票
class Dialog(QtWidgets.QDialog):
    def __init__(self):
        ...
        self.rows = []
        self.row_count = 1

    def add_row(self):
        label = QtWidgets.QLabel(f"#{self.row_count+1}")
        line_edit = QtWidgets.QLineEdit()
        push_button = QtWidgets.QPushButton("foo")

        self.rows.append([label, line_edit, push_button])
        for i, item in enumerate(self.rows[-1]):
            self.ui.gridLayout_5.addWidget(item, self.row_count, i, 1, 1)
        self.row_count += 1

我想你可以用这个实现你想要的东西。只是需要做一些调整才能正常工作,但我做不到,因为我没有这样的软件 .ui 文件。


0
投票

我用这个代码成功了。

def add_row(self):
    new_label = QtWidgets.QLabel(f"#{self.ui.row_count+1}")
    new_lineEdit1 = QtWidgets.QLineEdit()
    new_pushButton2 = QtWidgets.QPushButton()
    new_comboBox = QtWidgets.QComboBox()
    new_checkBox = QtWidgets.QCheckBox()
    new_lineEdit2 = QtWidgets.QLineEdit()
    new_pushButton1 = QtWidgets.QPushButton()


    self.ui.rows.append([new_label, new_lineEdit1, new_pushButton2, new_comboBox, new_checkBox, new_lineEdit2, new_pushButton1])
    for i, item in enumerate(self.ui.rows[-1]):
        self.ui.gridLayout_5.addWidget(item, self.ui.row_count, i, 1, 1)
    self.ui.row_count += 1

    new_comboBox.addItems(['123','456','qwerty'])
    new_comboBox.activated.connect(lambda: new_lineEdit1.setText(new_comboBox.currentText()))
    new_lineEdit2.returnPressed.connect(lambda: print(new_lineEdit2.text()))

最后一件事还不清楚: 比如说, 如果我需要把一个文本从... ... 行#3中的lineEdit2 到行外的其他lineEdit?与lineEdit3对话

我应该用什么来代替"???"?

self.ui.?????.textChanged.connect(lambda: self.ui.lineEdit3.setText(self.ui.?????.text())))
© www.soinside.com 2019 - 2024. All rights reserved.