📄 c2029.htm
字号:
<HTML><HEAD><TITLE>String Objects in Python and Qt</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="PyQt fundamentals"HREF="p1032.htm"><LINKREL="PREVIOUS"TITLE="Conclusion"HREF="x2026.htm"><LINKREL="NEXT"TITLE="String conversions"HREF="x2068.htm"></HEAD><BODYCLASS="CHAPTER"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"><AHREF="x2026.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x2068.htm"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH4">Chapter 8. String Objects in Python and Qt</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="c2029.htm#AEN2034">Introduction</A></DT><DT><AHREF="x2068.htm">String conversions</A></DT><DT><AHREF="x2104.htm">QCString — simple strings in PyQt</A></DT><DT><AHREF="x2183.htm">Unicode strings</A></DT></DL></DIV><P>Most likely, you won't need the information in this chapter very often. If you don't juggle character encodings on a regular basis, or work extensively with Unicode, then you can probably get by quite well with the automatic string handling of PyQt. However, situations may arise, especially when working with string-intensive applications, where PyQt's behavior might surprise you. Then you will probably find yourself coming to this chapter.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN2034">Introduction</A></H1><P>Working with strings is a delight in Python. Take the simple fact that you can, depending on your whim and on whether the string contains the other kind, choose to enclose your string literals in single ('), double ("), or triple (''' or """) quotes. Triple quoted strings can span multiple lines — no more string concatenations just because the string doesn't fit the line.</P><P>Once you have your string, and it can be a delightfully long string, megabytes if needs be, you can transform it, mangle it, search in it — all using a few choice modules, such as <TTCLASS="FILENAME">string</TT>, <TTCLASS="FILENAME">re</TT> or the native methods of the <TTCLASS="CLASSNAME">string</TT> object. About the only snag is the immutability of strings — every modifying action creates a new string from the old, which can be costly.</P><P>In C++, working with strings is <SPAN><ICLASS="EMPHASIS">not</I></SPAN> a delight. Working with strings in C++ requires using null-terminated character arrays, and writing all your own support functions. Or you have to try to use the C++ Standard Library String class, which is rather limited. This is why Trolltech created two string classes — <TTCLASS="CLASSNAME">QString</TT> and <TTCLASS="CLASSNAME">QCString</TT> — which are almost as powerful and friendly to use as the Python string classes. In fact, when Trolltech first created <TTCLASS="CLASSNAME">QString</TT>, there was no string class in the standard C++ library.</P><P>Python also has two string classes: the 'old' <TTCLASS="CLASSNAME">string</TT> class, in which every byte represents a character, and the newer Unicode <TTCLASS="CLASSNAME">string</TT> class, which contains a sequence of Unicode characters that can, depending on the encoding, take between one and four bytes. The Qt <TTCLASS="CLASSNAME">QString</TT> class is equivalent to the Python Unicode <TTCLASS="CLASSNAME">string</TT> class, and the Qt <TTCLASS="CLASSNAME">QCString</TT> class is more like the 'old' 8-bit Python <TTCLASS="CLASSNAME">string</TT>.</P><P>Your friendly Python Library Reference will tell you all about the <TTCLASS="FILENAME">string</TT> module, the <TTCLASS="FILENAME">string</TT> class, and the <TTCLASS="FILENAME">re</TT> module for regular expression matching. In this chapter I am more concerned with the interaction between <TTCLASS="CLASSNAME">QString</TT> and Python strings, and with character encoding questions.</P></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"><AHREF="x2026.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="book1.htm"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x2068.htm"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Conclusion</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="p1032.htm"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">String conversions</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -