📄 index.lxp@lxpwrap=x2568_252ehtm.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™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <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>Object and class introspection</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="Connecting signals and slots"HREF="x2549.htm"><LINKREL="NEXT"TITLE="Qt Class Hierarchy"HREF="c2591.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=x2549_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=c2591_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Object and class introspection</A></H1><P>Both Python and Qt offer a great deal of object introspection functionality — that is, methods of determining at runtime what kind of class an object is an instance of, or what methods an object implements. It has often been difficult to make Python and Qt introspection mesh well. One example is the <TTCLASS="CLASSNAME">QObject</TT>.<TTCLASS="FUNCTION">className()</TT>, which returns the name of the class of an object. Until PyQt version 2.5, this function always returned <TTCLASS="CLASSNAME">QObject</TT>, instead of the Python class name. Since that version, however, it returns the true class name:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 9-10. Object introspection using Qt</B></P><PRECLASS="SCREEN">Python 2.1 (#1, Apr 17 2001, 20:50:35) [GCC 2.95.2 19991024(release)] on linux2 Type "copyright", "credits" or "license"for more information. >>> from qt import *>>> t=QTimer() >>> t.className() 'QTimer'>>> class A(QTimer): pass ... >>> a=A()>>> a.className() 'A' >>> a.inherits('QTimer') 1 </PRE></DIV><P>For interesting Python introspection functions you should consult the Python language reference — but the equivalent using Python idioms of the above session would be:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 9-11. Object introspection using Python</B></P><PRECLASS="SCREEN"> >>> t.__class__ <class qt.QTimer at 0x8232cc4> >>> a.__class__ <class __main__.A at 0x826c2ac> >>> a.__class__.__bases__ (<class qt.QTimer at 0x8232cc4>,) </PRE></DIV><P>Object introspection is especially useful if you dabble in the black art known as meta-programming — that is, creating a program that run-time constructs some of the classes it needs. Heaps of fun — but not always innocent fun.</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=x2549_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=c2591_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Connecting signals and slots</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">Qt Class Hierarchy</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -