2

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)  

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.

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

EXAMPLE:

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:

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: 

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.

4

3 に答える 3

2

PyQt5 で削除された非推奨バージョンの SIGNAL/SLOT 実装を PyQt で使用していることに注意してください(また、PyQt4 での現在の実装でさえ多少バグがあります)。

可能であれば、コードの SIGNAL/SLOT 構文を変更します。

新しいシグナル/スロット メカニズムのしくみは次のとおりです。

class Worker(QThread):
    stateChanged = Signal(int)

    ....

    def some_method(self):
        self.stateChanged.emit(1)

GUIスレッドでは、同様に次のようなものがあります(worker = Worker()どこかに定義されていると仮定します:

class GUI(QDialog)

    worker = Worker()

    def __init__(self, parent=None):
        super(GUI, self).__init__(parent)
        self.worker.stateChanged.connect(self.my_desired_method)

    def my_desired_method(self, param):
        print param #prints out '1'

もちろん、このコードはそのままでは機能しませんが、PyQt (および PySide) でシグナル/スロットを処理する方法の一般的な概念です。

于 2014-04-04T09:18:27.090 に答える
0

これは役立つはずだと思います。

self.op = "point"
QtCore.QObject.connect(self.radioButton, QtCore.SIGNAL("clicked(bool)"),lambda : self.anyButton(self.radioButton.isChecked(),self.op))

def anyButton(self,kol,vv):
    print kol
    print vv

出力は

>>> 
True
point

これを見ることができます:信号に接続するときにPyQtがパラメータをスロットに送信する

于 2014-04-04T08:25:00.780 に答える