index.lxp@lxpwrap=x8887_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 454 行
HTM
454 行
<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>Creating .sip files</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="First Steps with Sip"HREF="a8834.htm"><LINKREL="PREVIOUS"TITLE="How sip works"HREF="x8867.htm"><LINKREL="NEXT"TITLE="Things sip can't do automatically"HREF="x8949.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=x8867_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Appendix C. First Steps with Sip</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x8949_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Creating .sip files</A></H1><P>The easiest way to understand the creation of the .sip input files is to look at an example. The process begins with the original header file from the library source code distribution (in this case, <TTCLASS="FILENAME">qmultilinedit.h</TT>) which is used as the starting point in creating the <TTCLASS="FILENAME">.sip</TT> file. It is a bit of a drudgery, since transforming a header file into an input file for <BCLASS="COMMAND">sip</B> is mostly handwork.</P><P>Shown is a fragment of the <TTCLASS="FILENAME">qmultilinedit.sip</TT> file created from <TTCLASS="FILENAME">qmultilinedit.h</TT>. If you look at the original file in the PyQt sources, you will find at the top of the complete <TTCLASS="FILENAME">qmultilinedit.sip</TT> some code for generating documentation, which is omitted here.</P><PRECLASS="PROGRAMLISTING">class QMultiLineEdit : QTableView{%HeaderCode#include <qmultilinedit.h>%Endpublic: QMultiLineEdit(QWidget * /TransferThis/ = 0,const char * = 0); int numLines() const; virtual void removeLine(int); void cursorPosition(int *,int *) const;%MemberCode // The Python interface returns a tuple. QMultiLineEdit *ptr; if (sipParseArgs(&sipArgsParsed,sipArgs, "m", sipThisObj,sipClass_QMultiLineEdit, &ptr)) { int line, col; ptr -> QMultiLineEdit::cursorPosition(&line,&col); return Py_BuildValue("(ii)",line,col); }%End </PRE><P>Most of the process of creating a .sip file is deleting all of the things SIP doesn't need or can't use. Typically all comments are stripped from the .h file in creating the .sip file, since they aren't necessary for SIP and are still available in the original .h file. For PyQt, SIP only uses methods and variables from specific parts of each class:</P><DIVCLASS="TABLE"></A><P><B>Table C-1. C++ access specifiers and sip</B></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THALIGN="LEFT"VALIGN="TOP">C++ access</TH><THALIGN="LEFT"VALIGN="TOP">Use in .sip file</TH></TR></THEAD><TBODY><TR><TDALIGN="LEFT"VALIGN="TOP">public</TD><TDALIGN="LEFT"VALIGN="TOP">methods and variables</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP">protected</TD><TDALIGN="LEFT"VALIGN="TOP">methods only</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP">private</TD><TDALIGN="LEFT"VALIGN="TOP">methods only</TD></TR></TBODY></TABLE></DIV><P>All private variables are deleted from the C++ header (.h) file, as are all protected variables. Public methods and variables are retained.</P><P>Normally all private methods are also deleted, but there are one or two cases where they are useful. For example, declaring a private copy constructor prevents SIP from automatically generating a public copy constructor.</P><P>Next, all parameter names are deleted. For instance:</P><PRECLASS="PROGRAMLISTING"> ... void cursorPosition( int *line, int *col ) const; ... </PRE><P>becomes</P><PRECLASS="PROGRAMLISTING"> ... void cursorPosition(int *,int *) const; ... </PRE><P><BCLASS="COMMAND">sip</B> does not ‘understand' (or need) method parameter names, and in fact any parameter names left in the <TTCLASS="FILENAME">.sip</TT> file will cause a <BCLASS="COMMAND">sip</B> syntax error when <BCLASS="COMMAND">sip</B> is run on the file. Note also that the ‘public' directive is removed from the class declaration line, as is any Q_OBJECT declaration or any friend class declarations when these are present. Any inline C++ code is also removed.</P><P>In the <TTCLASS="CLASSNAME">QMultiLineEdit</TT> constructor, the ‘parent' parameter name has been replaced with a <TTCLASS="FUNCTION">/TransferThis/</TT> directive. This directive tells <BCLASS="COMMAND">sip</B> that if the parameter is not <TTCLASS="FUNCTION">None</TT> then ownership of the <TTCLASS="CLASSNAME">QMultiLineEdit</TT> object is transferred from PyQt to Qt. Therefore Qt is responsible for deleting this <TTCLASS="CLASSNAME">QMultiLineEdit</TT> object at the appropriate time. Failure to include this directive where needed would result in a segmentation fault, usually when the program terminates and Python tries to destroy the object which Qt has already destroyed. A <TTCLASS="FUNCTION">/Transfer/</TT> directive is also available to serve a similar purpose for an an object passed as a parameter to an ordinary function or method.</P><P>Directly following the class declaration is a <TTCLASS="FUNCTION">%Headercode</TT> declaration that references the <TTCLASS="FILENAME">.h</TT> file that this <TTCLASS="FILENAME">.sip</TT> file was derived from. The <TTCLASS="FUNCTION">%Headercode</TT> declaration goes inside the class definition because <BCLASS="COMMAND">sip</B> generates a <TTCLASS="FILENAME">.h</TT>/<TTCLASS="FILENAME">.cpp</TT> file for every class. If there were multiple classes defined in <TTCLASS="FILENAME">qmultilinedit.h</TT>, each class would require a <TTCLASS="FUNCTION">%Headercode</TT> declaration. <BCLASS="COMMAND">Sip</B> itself doesn't use the <TTCLASS="FILENAME">.h</TT> file, but the <BCLASS="COMMAND">sip</B> generated code needs the <TTCLASS="FILENAME">.h</TT> file so it can know about the Qt classes and methods being wrapped.</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=x8867_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=x8949_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">How sip works</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=a8834_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Things <BCLASS="COMMAND">sip</B> can't do automatically</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?