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

📄 index.lxp@lxpwrap=x2068_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>String conversions</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 Objects in Python and Qt"HREF="c2029.htm"><LINKREL="NEXT"TITLE="QCString &#8212; simple strings in PyQt"HREF="x2104.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=c2029_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=x2104_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">String conversions</A></H1><P>I will return to      <TTCLASS="CLASSNAME">QCString</TT> later, as      <TTCLASS="CLASSNAME">QString</TT> is used everywhere in Qt.  All      user-visible text is set using <TTCLASS="CLASSNAME">QString</TT>s,      and if a widget returns some text, it will return a      <TTCLASS="CLASSNAME">QString</TT> as well. It should become clear      that the only way to work comfortably with strings in Python and      Qt is to have some automatic conversion between Python string      objects and <TTCLASS="CLASSNAME">QString</TT>s.    </P><P>The conversion from a Python string to a    <TTCLASS="CLASSNAME">QString</TT> is      completely transparent. Any Qt class method that asks for a      <TTCLASS="CLASSNAME">QString</TT> as an argument accepts any Python      string (or Python Unicode string). The other way around isn't      all that transparent, unfortunately.  When you ask,      for example, a <TTCLASS="CLASSNAME">QMultiLineEdit</TT> widget for its      contents, you get a <TTCLASS="CLASSNAME">QString</TT>. If you try to use      Python's regular expression engine on this object, of if you try      to write it to a file, you will be surprised at the results:    </P><DIVCLASS="EXAMPLE"></A><P><B>Example 8-1. qstring1.py &#8212; conversion from        <TTCLASS="CLASSNAME">QString</TT> to a Python string.</B></P><PRECLASS="PROGRAMLISTING">## qstring1.py - saving a QString to a file#from qt import *# Construct a Python stringpyString = """Now is the summer of our sweet content,Made o'er-cast winter by these Tudor clouds.And I that am not shaped for black-faced war,"""# Construct a Qt QStringqtString=QString("""I that am rudely cast and want true majesty,Am forced to fight,To set sweet England free.I pray to Heaven we fare well,And all who fight us go to Hell.""")f=open("richard", "w+")f.write(pyString)f.flush()f.write(qtString)f.close()      </PRE></DIV><P>If you run this script, you'll get the      following output:</P><PRECLASS="SCREEN">boud@calcifer:~/doc/opendoc/ch4 &#62; python qstring1.pyTraceback (most recent call last):  File "qstring1.py", line 26, in ?    f.write(qtString)TypeError: read-only character buffer, instanceboud@calcifer:~/doc/opendoc/ch4 &#62;    </PRE><P>There are good reasons for this behavior.      Returning <TTCLASS="CLASSNAME">QString</TT>s from widgets gives the      developer access to all the neat Qt string handling      functionality. A Qt string is mutable, in contrast to a Python      string, and having the Qt <TTCLASS="CLASSNAME">QString</TT> makes      it easier to change the contents in place. Lastly, returning a      Qt string instead of a Python string avoids a somewhat costly      conversion which might not be needed if all you want to do is to      stuff the text in another Qt widget.</P><P>Of course, the downside is that if you want      to treat a QString object as a Python string, you'll have to      convert it yourself, using one of the Python built-in functions      <TTCLASS="FUNCTION">str()</TT> or <TTCLASS="FUNCTION">unicode()</TT>.      Adapting the previous script makes it work as expected:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 8-2. qstring2.py - second try of saving a        <TTCLASS="CLASSNAME">QString</TT> to a file</B></P><PRECLASS="PROGRAMLISTING">## qstring2.py - saving a QString to a file#from qt import *# Construct a Python stringpyString = """Now is the summer of our sweet content,Made o'er-cast winter by these Tudor clouds.And I that am not shaped for black-faced war,"""# Construct a Qt QStringqtString=QString("""I that am rudely cast and want true majesty,Am forced to fight,To set sweet England free.I pray to Heaven we fare well,And all who fight us go to Hell.""")f=open("richard", "w+")f.write(pyString)f.flush()f.write(str(qtString))f.close()      </PRE></DIV><P>I don't need to show you screen output here      &#8212; it just works. You will have to pay attention to what      happens with the strings you receive from Qt widgets. If you      want to write the contents to a file, database, or to mangle the      string with Python modules, you will need to explicitly convert      the QString object to Python strings. If you want to feed the      string to another widget, you don't have to do anything.</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=c2029_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=x2104_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">String Objects in Python and Qt</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">QCString &#8212; simple strings in PyQt</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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