【问题标题】:How may I properly pass a unique string to a signal function? [duplicate]如何正确地将唯一字符串传递给信号函数? [复制]
【发布时间】:2019-04-25 15:30:52
【问题描述】:

尝试将唯一字符串传递给 QPushButton 单击槽失败。

使用 pyqt5,我正在遍历字符串参数列表以创建一个 UI 表单,其布局如下: QLabel QLineEdit QPushButton -- 列表中的每个参数一行。

QPushButton 的点击信号连接到打开 QFileDialog 的函数,允许用户为参数选择特定类型的文件或目录。参数一起传递:

 btn.clicked.connect(lamda: self.openFileDialog(param)
  1. 我已尝试创建/传递“param”字符串的唯一副本 btn.clicked.connect(lamda: self.openFileDialog(copy.copy(param))

  2. 我尝试在 for 循环范围之外使用字符串变量 即 p = 无 对于 idx,枚举中的参数(general_params_list): p = 参数 btn.clicked.connect(lamda: self.openFileDialog(p))

  3. 再次使用作用域 - 使用全局变量代替:self.p

  4. 我尝试创建和存储唯一按钮列表(而不是重新使用单个变量实例)。我在 for 循环之外创建了列表,并在循环中对其进行了初始化。

  5. 最后通过创建/存储/使用复制的“param”字符串列表进行扩充 (4)。

r = 0
ig1Layout = QGridLayout()

for idx, param in enumerate(general_params_list):
    paramLabel = QLabel(param)
    textLine = QLineEdit()
    btn = QPushButton("..")

    btn.clicked.connect(lambda: self.openFileDialog(param))

    ig1Layout.addWidget(paramLabel, r, 0)
    ig1Layout.addWidget(textLine, r, 1)
    ig1Layout.addWidget(btn, r, 2)

    r += 1


def openFileDialog(self, btnType):
    print("\nopenFileDialog() - clicked on '..' open dialog for: ", btnType)

预期的结果是每个函数槽都传递了一个唯一的参数字符串,以便我可以区分按下了哪个按钮。

实际结果是原始列表中的最后一个参数字符串被设置(传递)给所有按钮。即我正在传递/设置/使用对“参数”的引用而不是它的值??

【问题讨论】:

  • btn.clicked.connect(lambda: self.openFileDialog(param))更改为btn.clicked.connect(lambda checked, param=param: self.openFileDialog(param))

标签: python pyqt pyqt5


【解决方案1】:

试试看:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Window(QWidget):
    def __init__(self,parent=None):
        super(Window,self).__init__(parent)

        r = 0
        general_params_list = ("param 1", "param 2", "param 3")
        self.textLine_list = []

        ig1Layout = QGridLayout(self)

        for idx, param in enumerate(general_params_list):
            paramLabel = QLabel(param)
            self.textLine = QLineEdit() 
            self.textLine_list.append(self.textLine.text())

            self.btn = QPushButton("btn{}".format(idx))

            self.textLine.textChanged.connect(
                lambda text, idx=idx : self.on_text_changed(text, idx))  
            self.btn.clicked.connect(
                lambda state, param=param, i=idx : self.openFileDialog(param, i))                

            ig1Layout.addWidget(paramLabel,      r, 0)
            ig1Layout.addWidget(self.textLine,   r, 1)
            ig1Layout.addWidget(self.btn,        r, 2)
            r += 1

    def openFileDialog(self, btnType, i):
        print("\nopenFileDialog() - clicked on '..' open dialog for: ", btnType)
        print("{} -> {}".format(btnType, self.textLine_list[i]))

    def on_text_changed(self, text, idx):
        self.textLine_list[idx] = text


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

【讨论】:

  • 谢谢,谢谢!是的,这解决了它!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 2019-11-05
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多