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

📄 index.lxp@lxpwrap=x5879_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>The document</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="Creating Application Functionality"HREF="c5783.htm"><LINKREL="PREVIOUS"TITLE="The view"HREF="x5798.htm"><LINKREL="NEXT"TITLE="Saving and loading documents"HREF="x5925.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=x5798_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 17. Creating Application Functionality</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x5925_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">The document</A></H1><P>The <TTCLASS="CLASSNAME">KalamDocument</TT>      document class is a simple wrapper around a      <TTCLASS="CLASSNAME">QString</TT> object. The reason for using a      <TTCLASS="CLASSNAME">QString</TT> to hold all text, instead of a      Python string object, is straight-forward: with all the passing      around of strings, converting the      <TTCLASS="CLASSNAME">QString</TT>s retrieved from the      <TTCLASS="CLASSNAME">QMultiLineEdit</TT> widgets to Python strings      would take far too much time. String conversion is a fairly      costly operation, involving the copying of potentially large      amounts of memory.</P><P>There is another reason for using      <TTCLASS="CLASSNAME">QString</TT>in this manner: a      <TTCLASS="CLASSNAME">QString</TT> is mutable, and a Python string      not. This means, simply put, that every time you change a single      character in a Python string, a complete new copy of the string      is made, and the old copy is discarded.</P><P>There are good reasons for this behavior.      If Python strings were mutable, they could not be used to index      Python dictionaries based on their character value, but only on      the abstract object ID that all Python objects receive. Imagine      the mess you would have if you changed the actual value of a key      in a dictionary.</P><P><TTCLASS="CLASSNAME">QString</TT> is a clever      and optimized class. If two instances of      <TTCLASS="CLASSNAME">QString</TT> have the same contents, there is      a good chance that they will even share the memory needed to      store the actual text. Furthermore,      <TTCLASS="CLASSNAME">QString</TT> offers as rich a set of methods      to mangle strings as Python does, so we don't lose much      functionality. (But look carefully at the documentation for      <TTCLASS="CLASSNAME">QString</TT> &#8212; some functions, such as      <TTCLASS="FUNCTION">stripWhiteSpace()</TT>, return a new string      instead of working on the existing string.)</P><P>Would our editor have to store more      complex information, instead of plain text, we should use the      <TTCLASS="CLASSNAME">KalamDocument</TT> class to store its data      perhaps in a list of paragraphs, where each paragraph is a list      itself, containing words, lines and perhaps more complex      objects, such as images or even active widgets &#8212; so you      could embed a button or a hyperlink in your text. However, never      code what you don't yet need is a excellent motto...</P><PRECLASS="PROGRAMLISTING">"""kalamdoc.py - abstraction of a document with a certain encodingcopyright: (C) 2001, Boudewijn Remptemail:     boud@rempt.xs4all.nl"""from qt import *from resources import TRUE, FALSEclass KalamDoc(QObject):    """    The document represents a plain text with a certain encoding. Default    is Unicode.    signals: sigDocModified (boolean)             sigDocTitleChanged (string)             sigDocTextChanged (qstring, qwidget)    """    def __init__(self, *args):        apply(QObject.__init__, (self,)+args)        self.encoding="unicode"        self.newDocument()        self._fileName = None        self._title = "Untitled"        self._modified = FALSE        self._text = QString()    </PRE><P>Instead of wrapping a simple, silly      boolean value, we now wrap s single      <TTCLASS="CLASSNAME">QString</TT> object.</P><PRECLASS="PROGRAMLISTING">    def setText(self, text, view=None):        self._text=text        self._modified=TRUE        self.emit(PYSIGNAL("sigDocTextChanged"),                  (self._text, view))    </PRE><P>Most of the above functions haven't      changed the basic framework. Note that the      <TTCLASS="FUNCTION">slotModified</TT> function has disappeared.      Modifying a text isn't as simple as flipping a single      boolean.</P><P>The <TTCLASS="FUNCTION">setText</TT> function,      which is called from the <TTCLASS="CLASSNAME">KalamView</TT> class,      applies brute force to the text that      <TTCLASS="CLASSNAME">KalamDocument</TT> manages.  Quite simply,      <TTCLASS="FUNCTION">setText</TT> replaces the internal reference to      <TTCLASS="CLASSNAME">QString</TT> with a reference to the      <TTCLASS="CLASSNAME">QString</TT> that was presented by the calling      view. The text has been modified, and this is recorded in the      administrative <TTCLASS="VARNAME">_modified</TT> variable. Finally,      by emitting the <TTCLASS="VARNAME">"sigDocTextChanged"</TT> Python      signal, all views that show this document are told to update      their display.</P><P>The <TTCLASS="VARNAME">view</TT> parameter has      a default value of <TTCLASS="VARNAME">None</TT> &#8212; this means      the change does not originate with any view, and will be applied      to all views.</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=x5798_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=x5925_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">The view</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c5783_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Saving and loading documents</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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