📄 index.lxp@lxpwrap=x5722_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>A more complex view management solution</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="User Interface Paradigms"HREF="c5584.htm"><LINKREL="PREVIOUS"TITLE="A stack of documents"HREF="x5700.htm"><LINKREL="NEXT"TITLE="Conclusion"HREF="x5778.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=x5700_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 16. User Interface Paradigms</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x5778_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">A more complex view management solution</A></H1><P>In the previous section I suggested that it might be nice to code up a view manager where the list of open views was shown in a listbox on the left side. I could have have left this for you to do, but I couldn't resist.</P><PRECLASS="PROGRAMLISTING">"""listspace.py - stacked view manager with a list for the mdi frameworkcopyright: (C) 2001, Boudewijn Remptemail: boud@rempt.xs4all.nl"""from qt import *from resources import TRUE, FALSEclass ListSpace(QSplitter): </PRE><P>The <TTCLASS="CLASSNAME">ListSpace</TT> is based on <TTCLASS="CLASSNAME">QSplitter</TT> — that way the user can decide how wide he wants to have his list of window titles.</P><PRECLASS="PROGRAMLISTING"> def __init__(self, *args): apply(QSplitter.__init__,(self, ) + args) self.viewlist=QListBox(self) self.setResizeMode(self.viewlist, QSplitter.KeepSize) self.stack=QWidgetStack(self) self.views=[] self.connect(self.viewlist, SIGNAL("highlighted(int)"), self.__activateViewByIndex) </PRE><P>First the <TTCLASS="CLASSNAME">QListBox</TT> is added to the splitter, and then to the widget stack (which is used in the same way as in the previous section). Here, I chose to use a <TTCLASS="CLASSNAME">QListBox</TT>, because it offers a more comfortable interface for the adding, changing and removing of entries than a <TTCLASS="CLASSNAME">QListView</TT>. As soon as we need the treeview, column header or multi-column capabilities of <TTCLASS="CLASSNAME">QListView</TT>, the change to <TTCLASS="CLASSNAME">QListView</TT> will be simple enough.</P><P>Because the <TTCLASS="FUNCTION">highlighted(int)</TT> signal of <TTCLASS="CLASSNAME">QListBox</TT> passed the index of the selected entry in the listbox, not the actual view object, we have to pass it through an internal function, <TTCLASS="FUNCTION">__activateViewByIndex</TT>, which maps the index to the view object that should be activated.</P><PRECLASS="PROGRAMLISTING"> def addView(self, view): self.views.append(view) self.viewlist.insertItem(view.caption(), len(self.views) - 1) self.stack.addWidget(view, len(self.views) - 1) self.stack.raiseWidget(view) self.connect(view, PYSIGNAL("sigCaptionChanged"), self.setListText) def setListText(self, view, caption): i = self.views.index(view) self.viewlist.changeItem(caption, i) </PRE><P>Of course, adding a view is now slightly more complicated, because the caption of the view must also be inserted into the listbox. Note that we have changed the code of <TTCLASS="CLASSNAME">MDIView</TT> slightly: when its caption changes, it now emits a signal, which we use here to keep the title text in the listview synchronized with the title of the document. Synchronization is done using the <TTCLASS="FUNCTION">setListText</TT> function, which uses the view to determine the right entry in the listbox. Of course, the mapping between the view object and the entry in the listbox <SPAN><ICLASS="EMPHASIS">should</I></SPAN> be encapsulated in a subclass of <TTCLASS="CLASSNAME">QListBox</TT>.</P><PRECLASS="PROGRAMLISTING"> def removeView(self, view): if view in self.views: self.viewlist.removeItem(self.views.index(view)) self.stack.removeWidget(view) self.views.remove(view) </PRE><P>Removing an item from a <TTCLASS="CLASSNAME">QListView</TT> is rather difficult to do without clearing the entire listview and building the contents anew. <A href="index.lxp@lxpwrap=#FTN%2eAEN5763" name="AEN5763">[1]</A> Fortunately, the <TTCLASS="CLASSNAME">QListBox</TT> class offers a handy <TTCLASS="FUNCTION">remove()</TT> function.</P><PRECLASS="PROGRAMLISTING"> def activeWindow(self): return self.stack.visibleWidget() def cascade(self): pass def tile(self): pass def canCascade(self): return FALSE def canTile(self): return FALSE def windowList(self): return self.views def activateView(self, view): self.stack.raiseWidget(view) def __activateViewByIndex(self, index): self.activateView(self.views[index]) </PRE><P>Apart from <TTCLASS="FUNCTION">__activateViewByIndex()</TT>, which we discussed above, the rest of the code is a plain reflection of our view manager API — in other words, nothing spectacular.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><A href="index.lxp@lxpwrap=x5722_252ehtm.htm#AEN5763" name="FTN.AEN5763">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>This is one area where the cleverness of PyQt makes life a bit more difficult than you might like. In C++, you remove a <TTCLASS="CLASSNAME">QListViewItem</TT> by <TTCLASS="FUNCTION">deleting</TT> it. The parent <TTCLASS="CLASSNAME">QListView</TT> or <TTCLASS="CLASSNAME">QListViewItem</TT> then forgets about the child item, too. However, sip keeps a reference to the <TTCLASS="CLASSNAME">QListViewItem</TT>; deleting the item from Python won't make any difference—as long as the parent keeps a reference to the child, sip will keep one, too. There is a function <TTCLASS="FUNCTION">takeItem()</TT>, but its use is fraught with danger. You might want to try the <TTCLASS="FUNCTION">item.parent().removeChild(item)</TT> trick if you want to remove items from a <TTCLASS="CLASSNAME">QListView</TT>.</P></TD></TR></TABLE><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=x5700_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=x5778_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">A stack of documents</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c5584_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Conclusion</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -