⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.lxp@lxpwrap=x2549_252ehtm.htm

📁 GUI Programming with Python
💻 HTM
字号:
    <table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td>    <div align="center" id="bldcontent">      <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a>      <br>      <div class="symbol">Your OpenSource Publisher&#153;</div>    </div>      </td></tr></table>    <div align="center" class="author">      	<a href="../products.lxp">Products</a>	&nbsp;|&nbsp;	<a href="../wheretobuy.lxp">Where to buy</a>	&nbsp;|&nbsp;	<a href="../bookstore.lxp">Retailers</a>	&nbsp;|&nbsp;	<a href="../faq.lxp">FAQ</a>	&nbsp;|&nbsp;        <a href="../writeforus.lxp">Write for Us.</a>        &nbsp;|&nbsp;        <a href="#contact">Contact Us.</a>  </div>    <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%">      <div class="content">        <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%">          <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div>                    <HTML><HEAD><TITLE>Connecting signals and slots</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Python Objects and Qt Objects"HREF="c2341.htm"><LINKREL="PREVIOUS"TITLE="Other C++ objects"HREF="x2540.htm"><LINKREL="NEXT"TITLE="Object and class introspection"HREF="x2568.htm"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x2540_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 9. Python Objects and Qt Objects</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x2568_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Connecting signals and slots</A></H1><P>If you have worked with pointers in C++ or object references      in other languages, then you will probably have been wondering      whether creating signal/slot connections also means creating      object references. Remember, object A can register interest in      some signals Object B emits.</P><P>This means that somewhere there must be code that object B      calls when it wants to emit that signal; and that there must be      code that is called to notify object A. In order for that to be      possible, a reference must be stored to object A. This is known      as the observer pattern:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 9-9. sigslot.py - a simple signals/slots implementation        in Python, following the Observer pattern</B></P><PRECLASS="PROGRAMLISTING">## sigslot.py - a simple signals/slots implementation in Python#class ClassA:    def __init__(self):        self.interestedObjects=[]    def connect(self, obj):        self.interestedObjects.append(obj)    def sendSignal(self):        for obj in self.interestedObjects:            obj.slot("This is a signal from ClassA")class ClassB:    def slot(self, message):        print "Object with ID", id(self), "Got signal: message"objectA=ClassA()objectB=ClassB()objectA.connect(objectB)objectC=ClassB()objectA.connect(objectC)objectD=ClassB()objectA.connect(objectD)objectA.sendSignal()      </PRE></DIV><P>In this exceedingly simplified      implementation of the signals and slots concept, objectA      actually stores the references to interested objects. If the      PyQt signals and slots mechanism were implemented like this,      objects would not be deleted unless the all objects they had      connections to were deleted as well. This puts a burden on the      programmer, who would have to remember to sever all connections      by hand. We all know what happens when a programmer has to      remember cleaning up after him...</P><P>Fortunately, the implementation of signals      and slots in sip is not quite so basic. Sip works together with      the signals and slots implementation in Qt, which is highly      complex, and involves fooling around with a special-purpose      macro processor. This, at least, Python developers are      spared.</P><P>Sip keeps special proxy objects around to      handle the signal/slot connections. If you use a recent version      of Python (&#62;2.1), the actual connections will not need real      references, but can work with the new-fangled weak reference      concept. Weak references are references that don't count for the      purpose of reference counting.</P><P>This is good, because your application will      not crash if a signal is emitted that was connected to a slot in      a deleted object &#8212; and created connections will not keep      objects alive.</P><P><A href="index.lxp@lxpwrap=c1267_252ehtm.htm">Chapter 7</A> deals with signals and      slots in far more depth.</P></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=x2540_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=x2568_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Other C++ objects</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c2341_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Object and class introspection</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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