Qt provides several classes that lay out widgets on a form: QHBoxLayout, QVBoxLayout, QGridLayout, and QStackLayout.Other classes that perform layout management include QSplitter,QScrollArea,QMainWindow,and QWorkSpace.
A widget's size policy tells the layout system how it should stretch or shrink. Qt provides sensible default size policies for all its built-in widgets,but since no single default can account for every possible layout,it is still common for developers to change the size policies for one or two widgets on a form.A QSizePolicy has both a horizontal and a Vertical component.Here are the most useful values:
(1) Fixed means that the widget cannot grow or shrink.The widget always stays
at the size of its size hint.
(2) Minimum means that the widget's size hint is its minimum size.The widget
cannot shrink below the size hint,but it can grow to fill available space
if necessary.
(3) Maximum means that the widget's size hint is its maximum size.The widget
can be shrunk down to its minimum size hint.
(4) Preferred means that the widget's size hint is its preferred size,but that
the widget can still shrink or grow if necessary.
(5) Expanding means that the widget can shrink or grow and it is especially
willing to grow.
(6) Ignore is similar to Expanding,except that it ignores the widget's size
hint and minimum size hint.
The difference between Preferred and Expanding is that when a form that contains
both Preferred and Expanding widgets is resized,extra space is given to the Expanding
widgets,while the Preferred widgets stay at their size hint.
In addition to the size policy's horizontal and vertical components,the
QSizePolicy class stores a horizontal and a vertical stretch factor.These stretch
factors can be used to indicate that different child widgets should grow at
different rates when the form expands.
Yet another way of influcening a layout is to set a minimum size,a maximum size
,or a fixed size on the child widgets.
Splitters A Qsplitter is a widget that contains other widgets.The widgets in a splitter are
seperated by splitter handles.Users can change the sizes of a splitter's child
widgets by dragging the handles.Splitters can often be used as an alternative to
layout managers,to give more control to the user.For example:
int main(int argc,char *argv[])
{
QApplication app(argc,argv); QTextEdit *editor1 = new QTextEdit; QTextEdit *editor2 = new QTextEdit; QTextEdit *editor3 = new QTextEdit; QSplitter splitter(Qt::Horizontal); splitter.addWidget(editor1); splitter.addWidget(editor2); splitter.addWidget(editor3);
... splitter.show();
return app.exec();
}
|
阅读(1419) | 评论(0) | 转发(0) |