index.lxp@lxpwrap=x2693_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 486 行
HTM
486 行
<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>Application classes</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="Qt Class Hierarchy"HREF="c2591.htm"><LINKREL="PREVIOUS"TITLE="Base classes"HREF="x2629.htm"><LINKREL="NEXT"TITLE="Widget foundations: QWidget"HREF="x2765.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=x2629_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 10. Qt Class Hierarchy</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x2765_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Application classes</A></H1><P>Most gui applications consist of a main window embellished with toolbars, a menu bar, and a statusbar, and have a hole in the middle. The hole in the middle can be filled with a specialized widget, or it may be a sort of desktop-in-a- desktop, with its own windows, and with sub-windows that dock themselves to the sides of the main window. Often, and application includes a few secondary windows, dialogs and a number of small popup-windows that warn or inform the user.</P><P>We have already worked with a few of these components. There is <TTCLASS="CLASSNAME">QApplication</TT>, the base of every PyQt application. <TTCLASS="CLASSNAME">QApplication</TT> wants to receive the command-line arguments to determine the look and feel of the application, as shown in <A href="index.lxp@lxpwrap=x1067_252ehtm.htm">the Section called <I>As simple as they come</I> in Chapter 6</A>.</P><P>Then there's the main window — as shown in <A href="index.lxp@lxpwrap=x1149_252ehtm.htm">the Section called <I>A better Hello World</I> in Chapter 6</A>, you can have an unlimited number of main windows, and not all those main windows have to be of the same class, as long as they inherit <TTCLASS="CLASSNAME">QMainWindow</TT>.</P><P>We have yet to add the frills to the main window. PyQt makes it quite easy to do this. The best way of adding menu options and toolbar buttons is to create a <TTCLASS="CLASSNAME">QAction</TT> for each action. <TTCLASS="CLASSNAME">QAction</TT> is a class that brings together user interface information about a certain action the user can undertake in your application.</P><P>For instance, if you're developing a network client application, one of the actions could be the command to <BCLASS="COMMAND">log in</B>. Associated with this command is a short help text that appears as a tooltip, a longer help text that might appear in the status bar, an icon that is used in the toolbar, a short text for use in the menu, and an accelerator key that is used from the keyboard. The <BCLASS="COMMAND">log in</B> action can be enabled or disabled (when the network is down, for instance). You do not want to distribute all this functionality all over your application.</P><P>A <TTCLASS="CLASSNAME">QAction</TT> ties everything related to an action together, and can be added to toolbars and menus. When performed, a <TTCLASS="CLASSNAME">QAction</TT> emits an <TTCLASS="FUNCTION">activated()</TT> signal. The following is a simple example with an action, a menubar, a toolbar and a statusbar:</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-2. action.py - Using a QAction to group data associated with user commands</B></P><PRECLASS="PROGRAMLISTING">## action.py#import sysfrom qt import *connectIcon=["16 14 5 1", " c None", ". c black", "X c gray50", "o c red", "O c yellow", " ", " . ", " X .X ", " XooX . ", " Xoooo .X ", " XooooooX ", " XooooooX ", " XoooooX. ", " XooooX. ", " XOXXXX. ", " XOXX... ", " XOXX ", " XX ", " X " ]class MainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self, ) + args) self.setCaption("Network Client") # Define action self.action=QAction(self, "login") self.action.setText("Log in") self.action.setMenuText("&Login") self.action.setToolTip("Login to the central server") self.action.setWhatsThis("Logs in to the central server.") self.action.setStatusTip("Log in to the central server.") self.action.setAccel(Qt.CTRL + Qt.Key_L) self.action.setIconSet(QIconSet(QPixmap(connectIcon))) self.connect(self.action, SIGNAL("activated()"), self.slotAction) # Statusbar self.statusBar=QStatusBar(self) # Define menu self.menu=QPopupMenu() self.action.addTo(self.menu) self.menuBar().insertItem("&File", self.menu) # Define toolbar self.toolBar=QToolBar(self, 'Main') self.action.addTo(self.toolBar) # Set a central widget self.editor=QMultiLineEdit(self) self.setCentralWidget(self.editor) def slotAction(self): QMessageBox.information(self, "Network Client", "Connecting to server...")def main(args): app=QApplication(args) win=MainWindow() win.show() app.connect(app, SIGNAL("lastWindowClosed()") , app , SLOT("quit()") ) app.exec_loop()if __name__=="__main__": main(sys.argv) </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>action.py</P></DIV></P></DIV><P>When, in <A href="index.lxp@lxpwrap=c8100_252ehtm.htm">Chapter 24</A>, we reach the pinnacle of development of <SPANCLASS="APPLICATION">Kalam</SPAN>, the extensible Unicode editor, you will have become <SPAN><ICLASS="EMPHASIS">very</I></SPAN> familiar with <TTCLASS="CLASSNAME">QAction</TT>.</P><DIVCLASS="SECT2"><H2CLASS="SECT2">Multiple document windows with QWorkspace</A></H2><P>The MDI (multiple document interface) is a paradigm made popular by Microsoft, in which one application window contains several document windows. For certain classes of application, such as programming editors, this is a very comfortable paradigm, but most users tend to get very confused when confronted with windows that don't show up in their taskbar. In fact, a large percentage of users have trouble when there is more than one window on their desktop.</P><P>However, the functionality is available, and it might be useful for <SPAN><ICLASS="EMPHASIS">your</I></SPAN> application. Let's take our high-powered graphics editor, from the event1.py example, and give the user ten windows to scribble in. All that is needed is it to add the <TTCLASS="CLASSNAME">Painting</TT> to a <TTCLASS="CLASSNAME">QWorkspace</TT> object, instead of setting it as the central widget of the <TTCLASS="CLASSNAME">MainWindow</TT>.</P><P>Realistically, you'll want to offer menu options for selecting, tiling and cascading the windows. <TTCLASS="CLASSNAME">QWorkSpace</TT> provides a <TTCLASS="FUNCTION">tile()</TT> and a <TTCLASS="FUNCTION">cascade()</TT> slot for these purposes, as well as a <TTCLASS="FUNCTION">windowList</TT> that returns a list of all windows. While it is a bad idea to limit your users to a small maximum number of documents, if you let them open more, you should provide a separate window with a full list. Having more than ten windows to select from in a menu makes working difficult.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-3. fragment from mdi.py - ten little scribbling windows</B></P><PRECLASS="PROGRAMLISTING">...class MainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self,) + args) self.setCaption("MDI Scribbler") self.workspace=QWorkspace(self, "workspace") self.winlist=[] for i in range(10): win=Painting(self.workspace) win.resize(100,100) win.setCaption("Window " + str(i)) self.winlist.append(win) self.setCentralWidget(self.workspace)... </PRE></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>mdi.py - ten little scribbling windows.</P></DIV></P></DIV></DIV></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=x2629_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=x2765_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Base classes</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c2591_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Widget foundations: QWidget</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?