index.lxp@lxpwrap=x3410_252ehtm.htm

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

HTM
780
字号
        </P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3">QGrid</A></H3><P>It will come as no surprise that the          <TTCLASS="CLASSNAME">QGrid</TT> is a simple grid layout          manager &#8212; for more complicated layouts, with differently          sized columns, you really need          <TTCLASS="CLASSNAME">QGridLayout</TT>.        </P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3">QGroupBox</A></H3><P>A <TTCLASS="CLASSNAME">QGroupBox</TT>          gives you a frame (which can be drawn in as many flavors as          <TTCLASS="CLASSNAME">QFrame</TT> supports) and with a title          text which will appear on top of the frame. A          <TTCLASS="CLASSNAME">QGroupBox</TT> can hold child widgets.          Those widgets will be aligned horizontally, vertically or in          a grid. The grid can also be filled in columns (for          vertically oriented frames), or in strips (for horizontally          oriented frames).        </P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QLayout</A></H2><P><TTCLASS="CLASSNAME">QLayout</TT> is        foundation of all complex Qt layout managers. Built on        <TTCLASS="CLASSNAME">QLayout</TT>, there are three        layoutmanagers: one for horizontal layouts, one for vertical        layouts and one for grids. It's also quite possible to build a        new layoutmanager on <TTCLASS="CLASSNAME">QLayout</TT>, one, for        instance, that manages playing cards on a stack, or perhaps        items in circle.</P><P>You can not only add widgets to layouts;        but also layouts. In this way, quite complex layouts can        achieved with very little pain.</P><P>All layout managers work by maintaining        a list of layoutitems. Those items can be        <TTCLASS="CLASSNAME">QLayoutItem</TT>s,        <TTCLASS="CLASSNAME">QLayoutWidget</TT>s or        <TTCLASS="CLASSNAME">QSpacerItem</TT>s. It's interesting to note        that <TTCLASS="CLASSNAME">QLayout</TT> <SPAN><ICLASS="EMPHASIS">is</I></SPAN> a        <TTCLASS="CLASSNAME">QLayoutItem</TT>, and can thus be managed by        another layoutmanager.</P><P>Every layout item proxies for a widget.        A <TTCLASS="CLASSNAME">QSpacerItem</TT> is rather special, since        it doesn't represent a widget, but rather space. A        <TTCLASS="CLASSNAME">QSpacerItem</TT> &#8216;pushes' other        widgets, either horizontally or vertically. You can use them        to push all pushbuttons to the top of the dialog, instead of        having them spread out over the whole height by the layout        manager.      </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QBoxLayout and children</A></H2><P><TTCLASS="CLASSNAME">QBoxLayout</TT> is the        parent class of the horizontal and vertical box layout        managers &#8212; you will never use this class on its own, but        its useful to look at the methods it offers, because those are        inherited by <TTCLASS="CLASSNAME">QHBoxLayout</TT> and        <TTCLASS="CLASSNAME">QVBoxLayout</TT></P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">QGridLayout</A></H2><P>While you can handle many layout        problems with combinations of horizontal and vertical box        layout managers, other problems are more suited for a        grid-based layout. The <TTCLASS="CLASSNAME">QGridLayout</TT>        class provides a flexible layout manager.      </P><P>The grid managed by a        <TTCLASS="CLASSNAME">QGridLayout</TT> consists of cells laid out        in rows and columns: the grid cannot be as complicated as a        html table, but you can add widgets (or sub-layouts) that span        multiple rows and columns. Rows (or columns) can be given a        stretch factor and spacing.</P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-12. layout.py - two box layouts and adding and removing buttons          dynamically to a layout</B></P><PRECLASS="PROGRAMLISTING">## layout.py - adding and removing widgets to a layout#import sysfrom qt import *class MainWindow(QMainWindow):    def __init__(self, *args):        apply(QMainWindow.__init__, (self, ) + args)        self.setCaption("Adding and deleting widgets")        self.setName("main window")        self.mainWidget=QWidget(self) # dummy widget to contain the                                      # layout manager        self.setCentralWidget(self.mainWidget)        self.mainLayout=QVBoxLayout(self.mainWidget, 5, 5, "main")        self.buttonLayout=QHBoxLayout(self.mainLayout, 5, "button")        self.widgetLayout=QVBoxLayout(self.mainLayout, 5, "widget")        self.bnAdd=QPushButton("Add widget", self.mainWidget, "add")        self.connect(self.bnAdd, SIGNAL("clicked()"),                     self.slotAddWidget)        self.bnRemove=QPushButton("Remove widget",                                  self.mainWidget, "remove")        self.connect(self.bnRemove, SIGNAL("clicked()"),                     self.slotRemoveWidget)        self.buttonLayout.addWidget(self.bnAdd)        self.buttonLayout.addWidget(self.bnRemove)        self.buttons = []    def slotAddWidget(self):        widget=QPushButton("test", self.mainWidget)        self.widgetLayout.addWidget(widget)        self.buttons.append(widget)        widget.show()    def slotRemoveWidget(self):        self.widgetLayout.parent().removeChild(self.widgetLayout)        self.widgetLayout=QVBoxLayout(self.mainLayout, 5, "widget")        self.buttons[-1].parent().removeChild(self.buttons[-1])        del self.buttons[-1:]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><P>This example shows that it is not only        possible to dynamically add widgets to a layout, but also to        remove them again. Removing means first severing the link from        the widget to the parent, and then deleting (using        <TTCLASS="FUNCTION">del</TT>) all Python references to the widget.        When the last reference has been removed, the widget        disappears.</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>layout.py</P></DIV></P></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">setGeometry</A></H2><P>You can use        <TTCLASS="FUNCTION">setGeometry</TT> to set the size of every        individual widget yourself. There's another useful application        of <TTCLASS="FUNCTION">setGeometry()</TT>, too: if you save the        size of the application window when the last window closes in        a configuration file, you can bring the window back to its        last size and position the next time the user starts opens it.        </P><DIVCLASS="EXAMPLE"></A><P><B>Example 10-13. geometry.py - setting the initial size of an          application</B></P><PRECLASS="PROGRAMLISTING">## geometry.py#import sysfrom qt import *class MainWindow(QMainWindow):            def __init__(self, *args):        apply(QMainWindow.__init__, (self,) + args)        self.editor=QMultiLineEdit(self)        self.setCentralWidget(self.editor)        def main(args):    app=QApplication(args)    win=MainWindow()    win.setGeometry(100,100,300,300)    win.show()            app.connect(app, SIGNAL("lastWindowClosed()")                           , app                           , SLOT("quit()")                           )    app.exec_loop()    if __name__=="__main__":    main(sys.argv)        </PRE></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=x3270_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=x3581_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Advanced widgets</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">Dialogs and Standard Dialogs</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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