📄 index.lxp@lxpwrap=x2104_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>QCString — simple strings in PyQt</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="String Objects in Python and Qt"HREF="c2029.htm"><LINKREL="PREVIOUS"TITLE="String conversions"HREF="x2068.htm"><LINKREL="NEXT"TITLE="Unicode strings"HREF="x2183.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=x2068_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 8. String Objects in Python and Qt</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x2183_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">QCString — simple strings in PyQt</A></H1><P>Both Python and Qt have two types of strings: simple strings, which are sequences of bytes where every byte represents one character, and complex string objects, which contain characters in the Unicode encoding. Unicode is a complex topic that is treated in the next section; this section deals with simple strings.</P><P><TTCLASS="CLASSNAME">QCString</TT> is the PyQt equivalent of the Python simple string. The Qt documentation describes <TTCLASS="CLASSNAME">QCString</TT> as a weak class, which is accurate. The implementation does not feature all the intelligence and care that has gone into <TTCLASS="CLASSNAME">QString</TT>, and as a consequence it scales poorly to large strings.</P><P>As an abstraction of the standard C++ null-terminated string, <TTCLASS="CLASSNAME">QCString</TT> cannot contain any null bytes ('\0'). In this respect, <TTCLASS="CLASSNAME">QCString</TT> differs from the simple Python string object. The simple Python string is often used as a container for binary data, and the string object doesn't care whether it contains null bytes. Feeding a Python string that contains null bytes to a QCString provides interesting results: </P><DIVCLASS="EXAMPLE"></A><P><B>Example 8-3. empty.py - feeding zero bytes to a QCString</B></P><PRECLASS="PROGRAMLISTING">## empty.py - feeding zero bytes to a QCString#from qt import *pystring='abc\0def'print "Python string:", pystringprint "Length:", len(pystring)qcstring=QCString(pystring)print "QCString:", qcstringprint "Length:", qcstring.length() </PRE></DIV><P>Running the previous example produces the following output:</P><PRECLASS="SCREEN">boudewijn@maldar:~/doc/opendoc/ch4 > python empty.pyPython string: abcdefLength: 7QCString: abcLength: 3 </PRE><P>Except for this proviso, both <TTCLASS="CLASSNAME">QCString</TT> and the Python string object are equivalent, and you can use the Python string object wherever a <TTCLASS="CLASSNAME">QCString</TT> is needed as a parameter in a function. You can convert the <TTCLASS="CLASSNAME">QCString</TT> back to a python string with the <TTCLASS="FUNCTION">str()</TT> function. If the <TTCLASS="CLASSNAME">QCString</TT> is <SPAN><ICLASS="EMPHASIS">empty</I></SPAN>, i.e., it contains only one byte with the value <SPAN><ICLASS="EMPHASIS">zero</I></SPAN> ('\0'), an empty Python string is returned, <SPAN><ICLASS="EMPHASIS">not</I></SPAN> a Python string that contains one zero byte.</P><P>The issue of null versus empty strings is an interesting one. A null <TTCLASS="CLASSNAME">QCString</TT> is constructed as follows:</P><PRECLASS="PROGRAMLISTING">nullstring=QCString()</PRE><P>This string is conceptually equivalent to the Python <TTCLASS="CLASSNAME">None</TT> object, except that the null <TTCLASS="CLASSNAME">QCString</TT> has a type. There is no way to construct a <SPAN><ICLASS="EMPHASIS">null</I></SPAN> Python string: a Python string without contents is always <SPAN><ICLASS="EMPHASIS">empty</I></SPAN>, i.e. the equivalent of a <TTCLASS="CLASSNAME">QCString</TT> that contains one byte with the value <SPAN><ICLASS="EMPHASIS">zero</I></SPAN>. The following script attempts a few combinations, using Python's built-in <TTCLASS="FUNCTION">assert</TT> function. </P><DIVCLASS="TIP"><BLOCKQUOTECLASS="TIP"><P><B>Assert: </B>The <TTCLASS="FUNCTION">assert</TT> statement is one of the more useful tools in the Python developers toolchest. You can use <TTCLASS="FUNCTION">assert</TT> to check any statement for truth — and if it fails, an AssertionException is thrown. If you compile your Python scripts to optimized bytecode (.pyo files), then the assertion statements are removed, making <TTCLASS="FUNCTION">assert</TT> ideal for checking your code for invalid entry conditions in method calls during development. The use of <TTCLASS="FUNCTION">assert</TT> in the following script is more of a hack: this little script wouldn't do anything if run with python -O null.py; only the line print message, "TRUE" would be executed in the <TTCLASS="FUNCTION">assertTrue</TT> function.</P></BLOCKQUOTE></DIV><DIVCLASS="EXAMPLE"></A><P><B>Example 8-4. null.py - empty and null QCStrings and Python strings</B></P><PRECLASS="PROGRAMLISTING">## null.py - empty and null QCStrings and Python strings#from qt import QCString# this string is emptyemptypystring=""# this string contains one byte, zeronullpystring="\0"# this string is empty: it contains the empty string, terminated with \0emptyqcstring=QCString("")# this string is null: it doesn't contain datanullqcstring=QCString()def assertTrue(assertion, message): try: assert(assertion) print message, "TRUE" except AssertionError: print message, "FALSE"assertTrue(emptypystring==emptyqcstring, "Empty Python string equals empty QCString")assertTrue(emptypystring==str(emptyqcstring), "Empty Python string equals str(empty QCString)")assertTrue(emptypystring==str(nullqcstring), "Empty python string equals str(null QCString)")assertTrue(nullpystring==emptyqcstring, "Python string containing 0 byte equals empty QCString")assertTrue(nullpystring==str(emptyqcstring), "Python string containing 0 byte equals str(empty QCSTRING)")assertTrue(nullqcstring is None, "Null QCString equals None object") </PRE></DIV><P>Running this gives the following output:</P><PRECLASS="SCREEN">boudewijn@maldar:~/doc/opendoc/ch4 > python null.pyEmpty Python string equals empty QCString FALSEEmpty Python string equals str(empty QCString) TRUEEmpty python string equals str(null QCString) TRUEPython string containing 0 byte equals empty QCString FALSEPython string containing 0 byte equals str(empty QCSTRING) FALSENull QCString equals None object FALSE </PRE><P>Of course, some of these concerns hold for <TTCLASS="CLASSNAME">QString</TT>, too. It is equally possible to have an empty <TTCLASS="CLASSNAME">QString</TT> or a null <TTCLASS="CLASSNAME">QString</TT>. Note that embedding a zero byte in a Python string and then feeding it to a <TTCLASS="CLASSNAME">QString</TT> shows the same behavior as with <TTCLASS="CLASSNAME">QCString</TT>, even though <TTCLASS="CLASSNAME">QString</TT> isn't a null-terminated string class:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 8-5. emptyqstring.py - feeding zero bytes to a QString</B></P><PRECLASS="PROGRAMLISTING">## emptyqstring.py - feeding zero bytes to a QString#from qt import *pystring='abc\0def'print "Python string:", pystringprint "Length:", len(pystring)qstring=QString(pystring)print "QString:", qstringprint "Length:", qstring.length() </PRE></DIV><P>Look at the output:</P><PRECLASS="SCREEN">boudewijn@maldar:~/doc/opendoc/ch4 > python emptyqstring.pyPython string: abcdefLength: 7QString: abcLength: 3 </PRE><P>The unavoidable conclusion is that you shouldn't try to use Python strings as containers for binary data and then convert them to Qt string objects. Of course, there's a solution: you can use <TTCLASS="CLASSNAME">QByteArray</TT> to store binary data.</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=x2068_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=x2183_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">String conversions</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c2029_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Unicode strings</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -