index.lxp@lxpwrap=x5469_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 855 行 · 第 1/2 页
HTM
855 行
<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>The actual application</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="View"HREF="x5458.htm"><LINKREL="NEXT"TITLE="Conclusion"HREF="x5581.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=x5458_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=x5581_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">The actual application</A></H1><P>As with the document-view framework, you can view the <TTCLASS="CLASSNAME">QMainWindow</TT> derived class as the central application controller. It takes the user input and translates that to calls to either the data or the GUI interface.</P><P>Even though the <TTCLASS="CLASSNAME">MDIApp</TT> class might appear a bit complex (and certainly very long!) it is much simpler than it would be with everything from the <TTCLASS="CLASSNAME">DocManager</TT> added to it. The creation of <TTCLASS="CLASSNAME">QAction</TT>s, and the attendant fringe decorations such as menu's and toolbars, is quite standard:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 15-5. The application class</B></P><PRECLASS="PROGRAMLISTING">"""mdiapp.py — application class for the mdi frameworkcopyright: (C) 2001, Boudewijn Remptemail: boud@rempt.xs4all.nl"""from qt import *from mdiview import MDIViewfrom mdidoc import MDIDocfrom docmanager import DocManagerfrom resources import *class MDIApp(QMainWindow): """ MDIApp combines MDIDoc and MDIView into an single window, multiple sub-window, multiple document application. """ def __init__(self, *args): apply(QMainWindow.__init__,(self, ) + args) self.setCaption("MDI Application Framework") self.workspace = self.initWorkSpace() self.docManager=DocManager(self, self.workspace) self.connect(self.docManager, PYSIGNAL("sigNumberOfDocsChanged"), self.setActionsEnabled) self.initActions() self.initMenuBar() self.initToolBar() self.initStatusBar() self.setActionsEnabled() # # GUI initialization # def initActions(self): fileNewIcon=QIconSet(QPixmap(filenew)) fileQuitIcon=QIconSet(QPixmap(filequit)) fileOpenIcon=QIconSet(QPixmap(fileopen)) fileSaveIcon=QIconSet(QPixmap(filesave)) self.actions = {} self.actions["fileNew"] = QAction("New", fileNewIcon, "&New", QAccel.stringToKey("CTRL+N"), self) self.connect(self.actions["fileNew"], SIGNAL("activated()"), self.slotFileNew) self.actions["fileOpen"] = QAction("Open", fileOpenIcon, "&Open", QAccel.stringToKey("CTRL+O"), self) self.connect(self.actions["fileOpen"], SIGNAL("activated()"), self.slotFileOpen) self.actions["fileSave"] = QAction("Save", fileSaveIcon, "&Save", QAccel.stringToKey(""), self) self.connect(self.actions["fileSave"], SIGNAL("activated()"), self.slotFileSave) self.actions["fileSaveAs"] = QAction("Save as", fileSaveIcon, "&Save as", QAccel.stringToKey(""), self) self.connect(self.actions["fileSaveAs"], SIGNAL("activated()"), self.slotFileSaveAs) self.actions["fileClose"] = QAction("Close", "&Close Document", QAccel.stringToKey("CTRL+W"), self) self.connect(self.actions["fileClose"], SIGNAL("activated()"), self.slotFileClose) self.actions["fileQuit"] = QAction("Exit", fileQuitIcon, "E&xit", QAccel.stringToKey("CTRL+Q"), self) self.connect(self.actions["fileQuit"], SIGNAL("activated()"), self.slotFileQuit) self.actions["editDoc"] = QAction("Edit", fileQuitIcon, "&Edit", QAccel.stringToKey("CTRL+E"), self) self.connect(self.actions["editDoc"], SIGNAL("activated()"), self.slotEditDoc) self.actions["windowCloseWindow"] = QAction(self) self.actions["windowCloseWindow"].setText("Close Window") self.actions["windowCloseWindow"].setAccel(QAccel. stringToKey("CTRL+W")) self.actions["windowCloseWindow"].setMenuText("&Close Window") self.connect(self.actions["windowCloseWindow"], SIGNAL("activated()"), self.slotWindowCloseWindow) self.actions["windowNewWindow"] = QAction(self) self.actions["windowNewWindow"].setText("New Window") self.actions["windowNewWindow"].setMenuText("&New Window") self.connect(self.actions["windowNewWindow"], SIGNAL("activated()"), self.slotWindowNewWindow) self.actions["windowCascade"] = QAction(self) self.actions["windowCascade"].setText("Cascade") self.actions["windowCascade"].setMenuText("&Cascade") self.connect(self.actions["windowCascade"], SIGNAL("activated()"), self.workspace.cascade) self.actions["windowTile"] = QAction(self) self.actions["windowTile"].setText("Tile") self.actions["windowTile"].setMenuText("&Tile") self.connect(self.actions["windowTile"], SIGNAL("activated()"), self.workspace.tile) self.actions["windowAction"] = QActionGroup(self, None, FALSE) self.actions["windowAction"].insert(self.actions["windowCloseWindow"]) self.actions["windowAction"].insert(self.actions["windowNewWindow"]) self.actions["windowAction"].insert(self.actions["windowCascade"]) self.actions["windowAction"].insert(self.actions["windowTile"]) self.actions["helpAboutApp"] = QAction(self) self.actions["helpAboutApp"].setText("About") self.actions["helpAboutApp"].setMenuText("&About...") self.connect(self.actions["helpAboutApp"], SIGNAL("activated()"), self.slotHelpAbout) </PRE></DIV><P>The set of actions included in this framework is not complete, of course. Ideally, you would want accelerators for switching between views, and a lot of application specific actions. We'll be adding these over the next few chapters.</P><PRECLASS="PROGRAMLISTING"> def initMenuBar(self): self.fileMenu = QPopupMenu() self.actions["fileNew"].addTo(self.fileMenu) self.actions["fileOpen"].addTo(self.fileMenu) self.actions["fileSave"].addTo(self.fileMenu) self.actions["fileSaveAs"].addTo(self.fileMenu) self.actions["fileClose"].addTo(self.fileMenu) self.fileMenu.insertSeparator() self.actions["fileQuit"].addTo(self.fileMenu) self.menuBar().insertItem("&File", self.fileMenu) self.editMenu = QPopupMenu() self.actions["editDoc"].addTo(self.editMenu) self.menuBar().insertItem("&Edit", self.editMenu) self.windowMenu = QPopupMenu() self.windowMenu.setCheckable(TRUE) self.connect(self.windowMenu, SIGNAL("aboutToShow()"), self.slotWindowMenuAboutToShow) self.menuBar().insertItem("&Window", self.windowMenu) self.helpMenu = QPopupMenu() self.actions["helpAboutApp"].addTo(self.helpMenu) self.menuBar().insertItem("&Help", self.helpMenu) def initToolBar(self): self.fileToolbar = QToolBar(self, "file operations") self.actions["fileNew"].addTo(self.fileToolbar) self.actions["fileQuit"].addTo(self.fileToolbar) QWhatsThis.whatsThisButton(self.fileToolbar) def initStatusBar(self): self.statusBar().message("Ready...") </PRE><P>We have created menus, toolbars and statusbars so often by now that this is merely an exercise in cutting and pasting. However, note that we create a Window menu, but we don't add the actions to that menu. This is because the contents of the window menu are dynamic. Just before showing the window menu, when the signal <TTCLASS="FUNCTION">"aboutToShow()"</TT> is emitted, we will be building the menu from the list of views managed by the document manager. This is done in the <TTCLASS="FUNCTION">slotWindowMenuAboutToShow</TT> slot function.</P><PRECLASS="PROGRAMLISTING"> def initWorkSpace(self): workspace=QWorkspace(self) self.setCentralWidget(workspace) return workspace </PRE><P>For now, the view manager is simply an instance of <TTCLASS="CLASSNAME">QWorkSpace</TT>, which is a very simple class that manages widgets as sub-windows to itself. For it to manage widgets, they should be created with the workspace as parent. <TTCLASS="CLASSNAME">QWorkSpace</TT> has two methods: <TTCLASS="FUNCTION">activeWindow()</TT>, which returns the widget that currently has focus, and <TTCLASS="FUNCTION">windowList()</TT>, which returns the list of all windows.</P><P>Furthermore, there are two slots: <TTCLASS="FUNCTION">cascade()</TT> and <TTCLASS="FUNCTION">tile()</TT>, that arrange the widgets managed by the workspace. Lastly, there is one signal you can connect to: <TTCLASS="FUNCTION">windowActivated()</TT>, which is fired whenever a widget is activated — i.e. gets focus.</P><PRECLASS="PROGRAMLISTING"> def setActionsEnabled(self): enabled = self.docManager.numberOfDocuments() self.actions["fileSave"].setEnabled(enabled) self.actions["fileClose"].setEnabled(enabled) self.actions["editDoc"].setEnabled(enabled) </PRE><P>If there is no document loaded by the application, functions like ‘save', ‘close' or ‘edit' are not terribly relevant. It's better to disable them then. By requesting the number of documents managed by the document manager, we can easily achieve this. After all, no documents is zero, which is false for Python, and more than zero documents is always true.</P
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?