将参数传递给PyQt SIGNAL连接 [英] Passing argument to PyQt SIGNAL connection

查看:332
本文介绍了将参数传递给PyQt SIGNAL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将参数传递给PyQt4信号连接?就我而言,我有 n 个按钮,这些按钮设置了在界面上动态创建的相同菜单,具体取决于用户的输入:

Is it possible to pass an argument to PyQt4 Signal connections? In my case I have n buttons with set the same menu dinamically created on the interface, depending on user's input:

for j in range(0, len(self.InputList)):

    arrow = QtGui.QPushButton(Form)
    arrow.setGeometry(QtCore.QRect(350, 40*(j+3)+15, 19, 23))

    menu = QtGui.QMenu(Form)
    for element in SomeList:
        cb = QtGui.QRadioButton(element,menu)
        ca = QtGui.QWidgetAction(menu)
        ca.setDefaultWidget(cb)
        QtCore.QObject.connect(cb,QtCore.SIGNAL("stateChanged(int)"),self.SomeFunction)
    arrow.setMenu(menu)  

尽管进入界面的所有按钮的菜单都是相同的,但用户应该能够从任何按钮中选择一个值,并且对于任何一个按钮,操作都是相同的(将所选值添加到行编辑"),唯一的区别是行编辑可能是 1st 以及 2nd 3rd .

Although the menu is the same for all the buttons into the interface, the user should be able to select a value from any of the buttons and, for any of them, the action is the same ("add the selected value to a line edit") with the only difference that the line edit might be the 1st as well as the 2nd, the 3rd.

那么,我想问的是,是否有任何方法可以在此处传递参数 j :

What I would like to ask, then, is if there's any way to pass an argument j here:

QtCore.QObject.connect(cb,QtCore.SIGNAL("stateChanged(int)"),self.SomeFunction(j))

示例:

在此执行中,用户输入为3,因此我将进行3行编辑和具有相同菜单的3个按钮:

At this execution the user's inputs are 3, so I will have 3 line edits and three push buttons with the same menu:

Line Edit 1:
Line Edit 2:
Line Edit 3:

使用相同的功能SomeFunction,我想编辑Line Edits的值.因此,如果用户触摸第二行编辑附带的菜单,则应使用参数2 SomeFunction(2)调用功能SomeFunction,因此相同的方法将了解哪个行编辑是正确的方法:

Using the same function SomeFunction, I'd like to edit the value of the Line Edits. So if the user is touching the menu attached to the 2nd line edit, the function SomeFunction shall be called with the argument 2 SomeFunction(2), so the same method will understand itself which Line Edit is the right one:

Line Edit 1:
Line Edit 2: modified
Line Edit 3: 

我需要这个,因为主窗口上的行编辑"数量取决于用户选择的内容.我是一个新手,到目前为止,我一直为GUI中的任何对象创建一个函数,但是这次数字是动态的,并且我确信有一些更优雅的方法可以创建这种信号连接,虽然到目前为止我的文档阅读还不了解.

I need this because the number of Line Edits on the main window depends on what the user is selecting. I'm a newbie and so far I've always created one function for any object into the GUI, but this time the number is dynamic and I'm sure there are some more elegant ways to create this kind of signal connections, that I have not understood though so far from my documentations reading.

推荐答案

这里是一种不同的方法:与其将数据作为参数附加到信号处理程序,不如将其附加到菜单项本身.这提供了更大的灵活性,因为数据没有隐藏在匿名函数中,因此可以被应用程序的任何部分访问.

Here is a different approach: instead of attaching the data as an argument to the signal handler, attach it to the menu-item itself. This offers much greater flexibility, because the data is not hidden inside an anonymous function, and so can be accessed by any part of the application.

这很容易实现,因为Qt已经提供了必要的API.如果您采用这种方法,则示例代码如下所示:

It is very easy to implement, because Qt already provides the necessary APIs. Here is what your example code would look like if you took this approach:

        for j in range(0, len(self.InputList)):

            arrow = QtGui.QPushButton(self)
            arrow.setGeometry(QtCore.QRect(350, 40*(j+3)+15, 19, 23))

            menu = QtGui.QMenu(self)
            group = QtGui.QActionGroup(menu)
            for element in SomeList:
                action = menu.addAction(element)
                action.setCheckable(True)
                action.setActionGroup(group)
                action.setData(j)
            arrow.setMenu(menu)

            group.triggered.connect(self.SomeFunction)

    def SomeFunction(self, action):
        print(action.data())

这篇关于将参数传递给PyQt SIGNAL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆