index.lxp@lxpwrap=x1408_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 1,236 行 · 第 1/2 页

HTM
1,236
字号
      type-checking done. </P><P>Python signals are indicated by the      <TTCLASS="FUNCTION">PYSIGNAL()</TT> function, which also takes a      string. There is no <TTCLASS="FUNCTION">PYSLOT()</TT> function      corresponding to <TTCLASS="FUNCTION">SLOT()</TT>, because you can      use <SPAN><ICLASS="EMPHASIS">any</I></SPAN> function as a slot in      Python.</P><P>The argument of <TTCLASS="FUNCTION">PYSIGNAL()</TT> is a simple      string that is unique for the class from which the signal is      emitted. It performs the same function as the      <SPAN><ICLASS="EMPHASIS">occasion</I></SPAN> string in the small      <TTCLASS="FILENAME">registry.py</TT> script.  The difference is that      <TTCLASS="FUNCTION">PYSIGNAL()</TT> string needs to be unique only      for the class, and not the whole application.</P><P>Connecting to a Python signal doesn't      differ much from connecting to a C++ signal, except that you      don't have to worry so much about the type and number of      arguments of the signal. To rewrite the      <TTCLASS="FILENAME">registry.py</TT> example:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 7-6. Python signals and slots</B></P><PRECLASS="PROGRAMLISTING">## sigslot.py  &#8212; python signals and slots#from qt import *class Button(QObject):    def clicked(self):        self.emit(PYSIGNAL("sigClicked"), ())class Application(QObject):    def __init__(self):        QObject.__init__(self)        self.button=Button()        self.connect(self.button, PYSIGNAL("sigClicked"),                        self.doAppSpecificFunction)        self.connect(self.button, PYSIGNAL("sigClicked"),                        self.doSecondFunction)    def doAppSpecificFunction(self):        print "Function called"    def doSecondFunction(self):        print "A second function is called."app=Application()app.button.clicked()      </PRE></DIV><P>Running this example from the command line gives the      following output:</P><PRECLASS="SCREEN">/home/boudewijn/doc/pyqt/ch6 $ python sigslot.pyA second function is called.Function called    </PRE><P>The <TTCLASS="CLASSNAME">Button</TT> emits the      Python signal. Note the construction: the second argument to the      <TTCLASS="FUNCTION">emit</TT> function is a      <SPAN><ICLASS="EMPHASIS">tuple</I></SPAN> that contains the arguments you want      to pass on. It must always be a tuple, even if it has to be an      empty tuple, or a tuple with only one element. This is shown in      the next example, in which we have to explicitly create an empty      tuple, and a tuple with one element from a single argument, by      enclosing the argument in brackets and adding a comma:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 7-7. Python signals and slots with arguments</B></P><PRECLASS="PROGRAMLISTING">## sigslot2.py  &#8212; python signals and slots with arguments#from qt import *class Widget(QObject):    def noArgument(self):        self.emit(PYSIGNAL("sigNoArgument"), ())    def oneArgument(self):        self.emit(PYSIGNAL("sigOneArgument"), (1, ))    def twoArguments(self):        self.emit(PYSIGNAL("sigTwoArguments"), (1, "two"))class Application(QObject):    def __init__(self):        QObject.__init__(self)        self.widget = Widget()        self.connect(self.widget, PYSIGNAL("sigNoArgument"),                        self.printNothing)        self.connect(self.widget, PYSIGNAL("sigOneArgument"),                        self.printOneArgument)        self.connect(self.widget, PYSIGNAL("sigTwoArguments"),                        self.printTwoArguments)        self.connect(self.widget, PYSIGNAL("sigTwoArguments"),                        self.printVariableNumberOfArguments)    def printNothing(self):        print "No arguments"    def printOneArgument(self, arg):        print "One argument", arg    def printTwoArguments(self, arg1, arg2):        print "Two arguments", arg1, arg2    def printVariableNumberOfArguments(self, *args):        print "list of arguments", argsapp=Application()app.widget.noArgument()app.widget.oneArgument()app.widget.twoArguments()      </PRE></DIV><P>Note the usage of the *arg argument      definition. This Python construct means that a variable length      list of un-named arguments can be passed to a function. Thus      <TTCLASS="FUNCTION">printVariableNumberOfArguments(self, *args)</TT>      fits every signal that you care to connect it to.</P><P>It's an interesting test to run this script      several times: you will notice that the order in which the      signals generated by <TTCLASS="FUNCTION">twoArguments()</TT> arrive      at their destination is not fixed. This means that if a signal      is connected to two or more slots, the slots are not called in      any particular order. However, if two signals are connected to      two separate slots, then the slots are called in the order in      which the signals are emitted.</P><P>The following combinations of arguments to the      <TTCLASS="FUNCTION">connect()</TT> function are possible:</P><DIVCLASS="TABLE"></A><P><B>Table 7-1. Matrix of        <TTCLASS="CLASSNAME">QObject</TT>.<TTCLASS="FUNCTION">connect()</TT>        combinations.</B></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THWIDTH="50"ALIGN="LEFT"VALIGN="TOP">Signal            </TH><THWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Connected to            </TH><THWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              Syntax (Note that you can replace              <TTCLASS="FUNCTION">QObject.connect()</TT> by              <TTCLASS="FUNCTION">self.connect()</TT> everywhere.)            </TH></TR></THEAD><TBODY><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ slot of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"), object2,              SLOT("qtSlot()"))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ slot of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"), SLOT("qtSlot()"))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python slot of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"), object2,              pythonFunction)            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python slot of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"),              self.pythonFunction)            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ signal of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"), object2,              SIGNAL("qtSignal()")            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              C++            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ signal of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, SIGNAL("qtSignal()"), self,              SIGNAL("qtSignal()")            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ slot of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"), object2,              SLOT("qtSlot()"))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ slot of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"),              SLOT("qtSlot()"))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python slot of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"),              object2.pythonFunction))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python slot of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYTHON("pySignal()"),              self.pythonFunction))            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ signal of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, OYSIGNAL("pySignal()"), object2,              SIGNAL("qtSignal()")            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              C++ signal of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"), self,              SIGNAL("qtSignal()")            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python signal of another object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"), object2,              PYSIGNAL("pySignal()")            </TD></TR><TR><TDWIDTH="50"ALIGN="LEFT"VALIGN="TOP">              Python            </TD><TDWIDTH="100"ALIGN="LEFT"VALIGN="TOP">              Python signal of &#8216;self' object            </TD><TDWIDTH="100%"ALIGN="LEFT"VALIGN="TOP">              QObject.connect(object1, PYSIGNAL("pySignal()"),              PYSIGNAL("pySignal()")            </TD></TR></TBODY></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=c1267_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x1631_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Signals and Slots in Depth</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c1267_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Disconnecting</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?