PyQt - 带参数的插槽

发布于 2024-10-10 22:11:28 字数 579 浏览 8 评论 0 原文

示例:

我有三个按钮,所有的按钮几乎都一样。 我只想为所有 3 个按钮提供 1 个插槽功能。

def slotButtons(nr_button):

  #common part

  if(nr==1):
    #for button 1
  else if(nr==2):
    #for button 2
  else if(nr==3):
    #for button 3

  #common part 

所以我需要像带有参数的插槽之类的东西。Python

QtCore.QObject.connect(pushButton1, QtCore.SIGNAL("clicked()"), slotButtons(1))
QtCore.QObject.connect(pushButton2, QtCore.SIGNAL("clicked()"), slotButtons(2))
QtCore.QObject.connect(pushButton3, QtCore.SIGNAL("clicked()"), slotButtons(3))

(pyQt) 可以做这样的事情吗?

Example:

I have three pushbuttons, all makes almost the same.
I want to have only 1 slot-function for all 3 buttons.

def slotButtons(nr_button):

  #common part

  if(nr==1):
    #for button 1
  else if(nr==2):
    #for button 2
  else if(nr==3):
    #for button 3

  #common part 

So I need something like slots with parameter..

QtCore.QObject.connect(pushButton1, QtCore.SIGNAL("clicked()"), slotButtons(1))
QtCore.QObject.connect(pushButton2, QtCore.SIGNAL("clicked()"), slotButtons(2))
QtCore.QObject.connect(pushButton3, QtCore.SIGNAL("clicked()"), slotButtons(3))

Can Python(pyQt) do something that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞎闹 2024-10-17 22:11:28

connect 需要的是任何可调用 Python 对象。由于 Python 将函数作为一等对象,因此可以使用包装函数轻松实现。对于简单的情况,lambda 可以:

    self.connect(pyuic4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyuic4"))
    self.connect(pyrcc4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyrcc4"))

What connect needs is any callable Python object. Since Python has functions as first-class objects, this is easy to implement with a wrapper function. For simple cases, a lambda would do:

    self.connect(pyuic4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyuic4"))
    self.connect(pyrcc4Button, SIGNAL("clicked()"),
            lambda: self.setPath("pyrcc4"))
黯然 2024-10-17 22:11:28

您可以使用 QObject::sender() 函数对每个发送者、对象进行不同的行为,请参阅 QObject 文档了解详细信息。

You can use QObject::sender() function to behave differently for each sender, object, see QObject documentation for details.

束缚m 2024-10-17 22:11:28

我不使用PyQt,但看看QSignalMapper文档,它应该是相同的。基本上,它允许您将来自不同对象的信号映射到带有参数的插槽。

I don't use PyQt but take a look at QSignalMapper documentation, it should be the same. Basically, it allows you to map signals coming from different objects to a slot with a parameter.

痴者 2024-10-17 22:11:28

您可以使用一个成员变量来标识该对象并执行类似以下的操作,我确定。

def slotButtons(btn):
    if btn.who_are_you == 1:
        # Do some stuff
    elif btn.who_are_you == 2:
        # Do some stuff
    elif btn.who_are_you == 3:
        # Do some stuff

    # Common part

You could have a member variable to identify the object and do something like the following im sure.

def slotButtons(btn):
    if btn.who_are_you == 1:
        # Do some stuff
    elif btn.who_are_you == 2:
        # Do some stuff
    elif btn.who_are_you == 3:
        # Do some stuff

    # Common part
人│生佛魔见 2024-10-17 22:11:28

您甚至可以这样做:

lambda value: self.doStuff(True if value==True else False)

一旦您弄清楚它是如何工作的,它就是一个非常棒的工具。

编辑:
您还可以跳过所有 ifs/else...
直接开门见山... - 粗略的例子:

btn_01 = lambda: self.doStuff("C://")
btn_02 = lambda: self.doStuff("D://")

def doStuff(self,dir):
     saveFile(dir+"filename.txt")

You can even do :

lambda value: self.doStuff(True if value==True else False)

Its quite great tool once you figure out how it works.

Edit:
You can also skip doing all the ifs/else...
Just go straight to point... - rough example:

btn_01 = lambda: self.doStuff("C://")
btn_02 = lambda: self.doStuff("D://")

def doStuff(self,dir):
     saveFile(dir+"filename.txt")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文