python - Strange behavior using PyQt4's 'pyqtSlot' decorator before another decorator -


I have a strange problem with using the QtCore.pyqtSlot decorator in front of the function decorated with a different decorator. The problem below shows this problem, it immediately provides a low QT program with a single button. When pressing a button, the following happens:

(1) The signal 'signal_A' and 'signal_B' sequence are connected to method_A and method_B ​​respectively.

(2) signal_A and signal_B signals are emitted.

(3) The required behavior is that the method_A and method_B ​​are executed. However, it turns out that both signal_A and signal_B trigger method_A, or method_B ​​(oddly, this is nondeterministic, and different for each part of the program!).

It appears to be a bug.

This issue is present when PyQt4 binding is used in Qt. It is not present when using PySide (use the '-pyside' parameter to verify) I have seen both problems in Python 2 and 3, and both in Windows and Linux

This issue is related to the insertion of a decorator between the 'QT slot' and the law definition. In the examination program, 'null_decorator' should not do anything (if my understanding of decoration is correct), but when I remove it, the behavior of the program changes.

Any help to understand what's happening here is appreciated.

  #! To / Usr / bin / env python3 import sys, time that "--pyside not" sys.argv: # Alternatively, use: PyQt4 import QtCore, QtGui QtSignal = QtCore.pyqtSignal QtSlot = QtCore.pyqtSlot and PyQt4 binding # default PySide import PySide binding, QtGui QtSignal = QtCore.Signal QtSlot = QtCore.Slot def Null_decorator (f QtCore) to: def null_decorator_wrapper (self, * args, ** kwargs): return f (self, * args, ** kwargs) return null_decorator_wrapper class TestClass (QtCore.QObject): def __init __ (self, * args, ** kwargs): Super (TestClass, self) .__ init __ (* args, ** kwargs) @QtSlot () @null_decorator Def method_A (self): print ("method_A (executed)!") @QtSlot () @null_decorator def method_B ​​(self): executing print ( "method_B ​​()!") square DemonstrateProblemButton (QtGui.QPushButton): signal_A = QtSignal () signal_B = QtSignal () Def __init __ (self, * Arges, ** kwargs): Super (performance ProblemButton, self) .__ init __ (* args, ** kwargs) self.clicked.connect (self.on_clicked) @QtSlot () def On_clicked (self): # TestClass example create = TestClass () # signals connect .connect (instance.method_A) self.signal_B.connect (instance.method_B) # signal self.signal_A.emit (self.signal_A) self.signal_B.emit () Diif main (): # GUI application App = QtGui.QApication (sys.argv) Button = DemonstrateProblemButton ("Display Problem") button.sho W () returned QtGui.QApplication.exec_ () if __name__ == "__main__": exitcode = main () sys.exit (exitcode)  

null_decorator so that all the slots have the same name (i.e." Null_decorator_wrapper "), and therefore PyQt is not able to distinguish between them. It is possible.

In your example there are several ways to fix this.

First of all, you can be sure the slots different signature:

  @QtSlot () @null_decorator def method_A (self): print ( "method_A () execution! ") @ QT slot (Int) @ Nl_ Disikoret def method_B ​​(self, foo = 0): print (" Method_B ​​() execution! ")  

second, clear You can specify slot names as follows:

  @QtSlot (name = "method_A") @ Null_decorator def method_A (auto): print ("method_A () execution!") @ Koutslot (name = "Method_B") @null_decorator def method_B ​​(auto, foo = 0): print ("method_B ​​( ) Execution! ")  

Third, you can set the name in null_decorator automatically:

  def null_decorator ( f): def null_decorator_wrapper (self, * Algs, ** Cwarj): return F (self, AGR, ** Cwargs) null_decorator_wrapper .__ Nam__ = f .___ Nam__ return null_decorato R_wrapper  

PS : pyqtSlot decorator behavior has clearly stated (in particular, name parameter), and certainly not a bug is.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -