通过 connect 传递额外的参数

Passing extra arguments through connect(通过 connect 传递额外的参数)
本文介绍了通过 connect 传递额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否可以通过插槽传递变量,以便我可以打印出某些文本?试图将另一个函数中定义的变量DiffP"传递给插槽.

Is it possible to pass variables through slots so I can print out certain text? Trying to pass variable 'DiffP' which is defined in another function to slot.

DiffP"会根据选择的文件而变化.

'DiffP' changes based on which file is selected.

def addLineEdit(self):
    try:
        self.clearLayout()
        self.FileButton ={}
        self.Input = {}
        self.TotalInput = []
        for i in range(int(self.numberLine.text())):
            self.FileButton[i] = QtWidgets.QPushButton(self.centralwidget)
            self.FileButton[i].setText('Case {}'.format(i+1))
            self.FileButton[i].setFlat(True)
            self.FileButton[i].setMaximumSize(QtCore.QSize(50, 50))
            self.hboxlayout[0].addWidget(self.FileButton[i])
            self.FileButton[i].clicked.connect(lambda i=i: self.openfile(i))
            self.buttonGroup.addButton(self.FileButton[i],i)
            self.buttonGroup.buttonClicked['int'].connect(self.input)

def searchfile(self,dir):
        with open(dir) as f:
            content = f.readlines()
            MainList = content[44].split()
            RPM = round(float(MainList[0]), 2)
            Ps = round(float(MainList[1]), 2)
            Ts = round(float(MainList[2]), 2)
            Pd = round(float(MainList[3]), 2)
            Ratio = round(Pd / Ps, 2)
            DiffP = round(Pd - Ps, 2)
@pyqtSlot(int)
def input(self,button_or_id,DiffP):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(str(DiffP))
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')

def openfile(self,i):
    filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Choose file')
    dir = filename[0]
    directory = os.path.split(dir)[0]
    return self.searchfile(dir)

推荐答案

问题可以通过两种方式解决:

The problem can be solved in 2 ways:

一般:

    obj.signal.connect(lambda param1, param2, ..., arg1=val1, arg2= value2, ... : fun(param1, param2,... , arg1, arg2, ....))

def fun(param1, param2,... , arg1, arg2, ....):
    [...]

地点:

  • param1, param2, ... : 是信号发送的参数
  • arg1, arg2, ...: 是你要花费的额外参数

在你的情况下:

    self.buttonGroup.buttonClicked['int'].connect(lambda i: self.input(i, "text"))

@pyqtSlot(int)
def input(self, button_or_id, DiffP):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(DiffP)
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')

<小时>

使用functools.partial:

一般:


Using functools.partial:

In general:

    obj.signal.connect(partial(fun, args1, arg2, ... ))

def fun(arg1, arg2, ..., param1, param2, ...):
    [...]

地点:

  • param1, param2, ... : 是信号发送的参数
  • arg1, arg2, ...:是您要发送的额外参数

在你的情况下:

from functools import partial

    [...]
    self.buttonGroup.buttonClicked['int'].connect(partial(self.input, "text"))


@pyqtSlot(int)
def input(self, DiffP, button_or_id):
    if isinstance(button_or_id, int):
        if button_or_id == 0:
            self.TotalInput[0].setText(DiffP)
        elif button_or_id == 1:
            self.TotalInput[54].setText('1')

这篇关于通过 connect 传递额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Multiprocessing on Windows breaks(Windows 上的多处理中断)
How to use a generator as an iterable with Multiprocessing map function(如何将生成器用作具有多处理映射功能的可迭代对象)
read multiple files using multiprocessing(使用多处理读取多个文件)
Why does importing module in #39;__main__#39; not allow multiprocessig to use module?(为什么在__main__中导入模块不允许multiprocessig使用模块?)
Trouble using a lock with multiprocessing.Pool: pickling error(使用带有 multiprocessing.Pool 的锁时遇到问题:酸洗错误)
Python sharing a dictionary between parallel processes(Python 在并行进程之间共享字典)