index.lxp@lxpwrap=x5388_252ehtm.htm

来自「GUI Programming with Python」· HTM 代码 · 共 576 行 · 第 1/2 页

HTM
576
字号
    <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 Manager</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="A More Complex Framework: Multiple Documents, Multiple    Views"HREF="c5288.htm"><LINKREL="PREVIOUS"TITLE="Document/View Manager"HREF="x5339.htm"><LINKREL="NEXT"TITLE="Document"HREF="x5451.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=x5339_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. A More Complex Framework: Multiple Documents, Multiple    Views</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x5451_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">The Document Manager</A></H1><P>The <TTCLASS="CLASSNAME">DocManager</TT>      class is one of the more complex classes discussed in this book.    </P><DIVCLASS="EXAMPLE"></A><P><B>Example 15-2. The document manager class</B></P><PRECLASS="PROGRAMLISTING">"""    docmanager.py &#8212; manager class for document/view mappingscopyright: (C) 2001, Boudewijn Remptemail:     boud@rempt.xs4all.nl"""from qt import *TRUE=1FALSE=0class DocManagerError(Exception):passclass NoSuchDocumentError(DocManagerError):    ERR = "Document %s with title %s is not managed by this DocumentManager"    def __init__(self, document):        self.errorMessage = ERR % (str(document), document.title(), str())    def __repr__(self):        return self.errorMessage    def __str__(self):        return self.errorMessageclass DocumentsRemainingError(DocManagerError):    def __init__(self, document):        self.errorMessage = "There are still documents remaining."    def __repr__(self):        return self.errorMessage    def __str__(self):        return self.errorMessage    </PRE></DIV><P>If you have a complex class like the      document manager, it is often useful to create a few specific      exception classes. You can still <TTCLASS="FUNCTION">raise</TT>      exceptions that will be mere messages in a string &#8212; but      these have been deprecated since Python 2.0. For the document      manager we have a small hierarchy of exceptions, with a base      exception (<TTCLASS="CLASSNAME">DocManagerError</TT>), and two      specific exceptions, <TTCLASS="CLASSNAME">NoSuchDocumentError</TT>      and <TTCLASS="CLASSNAME">DocumentsRemainingError</TT>. The first      exception is raised when an attempt is made to delete a document      which is not managed by the document manager. This can happen      when you need more than one document manager, for instance. The      second is raised when an attempt is made to delete all open      documents, but one or more of them could not be closed.</P><PRECLASS="PROGRAMLISTING">class DocManager(QObject):    """    The DocManager manages the creation and removal of documents    and views.    """    def __init__(self, parent, viewManager = None):        QObject.__init__(self)        self._viewToDocMap = {}        self._docToViewMap = {}        self._parent=parent        if viewManager:            self._viewManager = viewManager        else:            self._viewManager = parent    </PRE><P>Two very simple datastructures manage all      the information in the document manage.  The first is      <TTCLASS="VARNAME">_viewToDocMap</TT>, which maps documents to views      (one document can be associated with a list of views). The other      datastructure, <TTCLASS="VARNAME">_docToViewMap</TT>, maps views to      documents. Note the single underscore before the variable names;      this indicates that you shouldn't try to use the variable      outside its class, in this case      <TTCLASS="CLASSNAME">DocManager</TT>. The      <TTCLASS="VARNAME">viewManager</TT> is the object that collects all      views and shows them in the application letterbox between      toolbars and statusbars.</P><PRECLASS="PROGRAMLISTING">    def numberOfDocuments(self):        return len(self._docToViewMap)    def numberOfViews(self):        return len(self._viewToDocMap)    def views(self, document):        return self._docToViewMap[document]    def _createView(self, document, viewClass):        view = viewClass(self._viewManager,                         document,                         None,                         QWidget.WDestructiveClose)        view.installEventFilter(self._parent)        if self._viewToDocMap == {}:            view.showMaximized()        else:            view.show()        if self._docToViewMap.has_key(document):            index = len(self._docToViewMap[document]) + 1        else:            index = 1        view.setCaption(document.title() + " %s" % index)        return view    </PRE><P>The function <TTCLASS="FUNCTION">_createView(self,        document, viewClass)</TT> not only maps views to      documents, but also creates the view objects. Note the      <TTCLASS="VARNAME">QWidget.WDestructiveClose</TT> flag &#8212; if      this is not passed to the <TTCLASS="CLASSNAME">QWidget</TT>-derived      view class, the view will not disappear from the screen when

⌨️ 快捷键说明

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