首页 > 解决方案 > 如何通过单击按钮复制整行小部件,然后访问新的小部件?

问题描述

假设我们有一个对话框,其中包含用户更改的一行参数。

对话

这些参数之间有一些联系。用户可以根据需要多次添加带有“添加类型”按钮的行。目的是在单击按钮时复制整行小部件,保存小部件之间的连接,并有机会访问此参数(在此示例中 - 打印 2nd lineEdit 的文本,包括解释器中新行中的文本。

请帮助我的函数 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

标签: pythonpython-3.xpyqtpyqt5qt-designer

解决方案


我已成功使用此代码:

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())))

推荐阅读