index.lxp@lxpwrap=x3410_252ehtm.htm
来自「GUI Programming with Python」· HTM 代码 · 共 780 行 · 第 1/2 页
HTM
780 行
<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>Layout managers</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="Advanced widgets"HREF="x3270.htm"><LINKREL="NEXT"TITLE="Dialogs and Standard Dialogs"HREF="x3581.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=x3270_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=x3581_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Layout managers</A></H1><P>One of the great strengths of PyQt is the use of layout managers. Formerly, gui designers had to position and size every element in their dialogs with pixel precision. Of course, this meant that enlarging a window wouldn't show the user more data, just a vast desert of boring grey pixels. Worse, when making a window smaller, data would be obscured. Even worse, there are still applications being made where you cannot resize the windows <SPAN><ICLASS="EMPHASIS">at all</I></SPAN>. </P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Too large...</P></DIV></P></DIV><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>Too small.</P></DIV></P></DIV><P>It's easy to write applications as badly behaved as this in PyQt— but where a Visual Basic developer has to write a complex resize routine that recalculates the size and position of each element, PyQt developers can use Qt's advanced layout management facilities.</P><P>Basically, this means that you create several containers that hold your widgets, and those widgets will resize together with the containers. The easiest way to create a pleasing layout is by using the BlackAdder or Qt forms designer, as this automatically uses sensible defaults.</P><P>There are three fundamental approaches to layout management in PyQt: by stacking widgets or grouping them in frames, by using the simple layout management provided by <TTCLASS="CLASSNAME">QFrame</TT> and children, or by using the advanced layout management <TTCLASS="CLASSNAME">QLayout</TT> provides. In Qt 3.0 <TTCLASS="CLASSNAME">QLayout</TT> is even smart enough to reverse the order of labels and entry widgets in dialog boxes for right-to-left scripts.</P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B><TTCLASS="CLASSNAME">QMainWindow</TT> provides its own layout management— it manages the size and position of the menubar, toolbar or toolbars, statusbar and the widget in the middle. If that widget is not composed of several widgets, the management will be quite sufficient. If there are several widgets constrained by a <TTCLASS="CLASSNAME">QSplitter</TT>, the management will likewise be sufficient, because in that case, the <TTCLASS="CLASSNAME">QSplitter</TT> will be the central widget. If you have a more complex assembly of widgets, you will have to create a dummy central <TTCLASS="CLASSNAME">QWidget</TT> that contains a layoutmanager that manages those widgets in a pleasing way. You can also directly add a layout manager to QMainWindow, but PyQt will natter about a layout manager being added to a widget that already had one. It's not dangerous, though. See <A href="index.lxp@lxpwrap=x3410_252ehtm.htm#CH2EXAMPLELAYOUT">Example 10-12</A> for an example of such a dummy central widget.</P></BLOCKQUOTE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Widget sizing: QSizePolicy</A></H2><P><TTCLASS="CLASSNAME">QWidget</TT> based classes provide the layout management system with <SPAN><ICLASS="EMPHASIS">size hints</I></SPAN>. This is a subtle system based on a class named <TTCLASS="CLASSNAME">QSizePolicy</TT>. A widget's size policy determines how small a widget can shrink, how big it can grow and how big it really wants to be. Then the layout manager negotiates with the widget though the use of the <TTCLASS="FUNCTION">sizeHint()</TT> about the size it will get.</P><P>A widget can thus indicate whether it prefers to stay a fixed horizontal or vertical size, or would like to grow to occupy all available space. QSizePolicy contains a horizontal size policy record and a vertical size policy record. You can set the size policy programmatically, but the setting is also available in the BlackAdder forms creator.</P><P>The following size policies exist:</P><P></P><UL><LI><P>Fixed — the widget can't shrink nor grow.</P></LI><LI><P>Minimum — the widget can't shrink, and shouldn't grow.</P></LI><LI><P>Maximum — the widget can't grow, but can shrink without any problem.</P></LI><LI><P>Preferred — the widget can shrink, but shouldn't grow.</P></LI><LI><P>MinimumExpanding — the widget can't shrink, but should be allowed to grow as much as possible.</P></LI><LI><P>Expanding — the widget can shrink, but should be allowed to grow as much as possible.</P></LI></UL></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Groups and frames</A></H2><P>One way of getting automatic layout management is by using <TTCLASS="CLASSNAME">QFrame</TT>, and its children, like <TTCLASS="CLASSNAME">QGroupBox</TT>. We have already seen such a frame in the radiobuttons example, <A href="index.lxp@lxpwrap=x2976_252ehtm.htm#CH2EXAMPLERADIOBUTTONS">Example 10-9</A>. The contents of the frame will be managed automatically. </P><P><TTCLASS="CLASSNAME">QFrame</TT> has three interesting child classes: <TTCLASS="CLASSNAME">QGrid</TT>, <TTCLASS="CLASSNAME">QHBox</TT> and <TTCLASS="CLASSNAME">QGroupBox</TT>. There's also <TTCLASS="CLASSNAME">QVBox</TT>, which descends from <TTCLASS="CLASSNAME">QHBox</TT>. </P><P>Adding widgets to one of the frame-based layout managers is simply a matter of creating the widget with the layout manager as <SPAN><ICLASS="EMPHASIS">parent</I></SPAN>. Those widgets will be resized according to the value their <TTCLASS="FUNCTION">sizeHint()</TT> returns.</P><DIVCLASS="SECT3"><H3CLASS="SECT3">QHBox</A></H3><P>This a very, very simple class. A <TTCLASS="CLASSNAME">QHBox</TT> aligns its children horizontally, with a settable spacing between them. </P></DIV><DIVCLASS="SECT3"><H3CLASS="SECT3">QVBox</A></H3><P>A <TTCLASS="CLASSNAME">QVBox</TT> layout is possibly even simpler than the <TTCLASS="CLASSNAME">QHBox</TT> layout: as the name implies, it aligns its children vertically.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?