这个是官方的文档,现在还没有翻译,有时间自己会把这个好好的翻译一下。

QFormLayout类是用来管理表格的输入部件以及和它们相关联的标签。

也就是说QFormLayout这个布局一般情况下是用来在里面添加具有输入功能的控件并且给添加的控件配备一个与之相关联的标签。

 #include <QFormLayout>

 QT += widget

成员函数列表

Public Types

enum FieldGrowthPolicy { FieldsStayAtSizeHint, ExpandingFieldsGrow, AllNonFixedFieldsGrow }
enum ItemRole { LabelRole, FieldRole, SpanningRole }
enum RowWrapPolicy { DontWrapRows, WrapLongRows, WrapAllRows }

Properties

  • 2 properties inherited from QLayout
  • 1 property inherited from QObject

Public Functions

  QFormLayout(QWidget * parent = 0)
  ~QFormLayout()
void addRow(QWidget * label, QWidget * field)
void addRow(QWidget * label, QLayout * field)
void addRow(const QString & labelText, QWidget * field)
void addRow(const QString & labelText, QLayout * field)
void addRow(QWidget * widget)
void addRow(QLayout * layout)
FieldGrowthPolicy fieldGrowthPolicy() const
Qt::Alignment formAlignment() const
void getItemPosition(int index, int * rowPtr, ItemRole * rolePtr) const
void getLayoutPosition(QLayout * layout, int * rowPtr, ItemRole * rolePtr) const
void getWidgetPosition(QWidget * widget, int * rowPtr, ItemRole * rolePtr) const
int horizontalSpacing() const
void insertRow(int row, QWidget * label, QWidget * field)
void insertRow(int row, QWidget * label, QLayout * field)
void insertRow(int row, const QString & labelText, QWidget * field)
void insertRow(int row, const QString & labelText, QLayout * field)
void insertRow(int row, QWidget * widget)
void insertRow(int row, QLayout * layout)
QLayoutItem * itemAt(int row, ItemRole role) const
Qt::Alignment labelAlignment() const
QWidget * labelForField(QWidget * field) const
QWidget * labelForField(QLayout * field) const
int rowCount() const
RowWrapPolicy rowWrapPolicy() const
void setFieldGrowthPolicy(FieldGrowthPolicy policy)
void setFormAlignment(Qt::Alignment alignment)
void setHorizontalSpacing(int spacing)
void setItem(int row, ItemRole role, QLayoutItem * item)
void setLabelAlignment(Qt::Alignment alignment)
void setLayout(int row, ItemRole role, QLayout * layout)
void setRowWrapPolicy(RowWrapPolicy policy)
void setSpacing(int spacing)
void setVerticalSpacing(int spacing)
void setWidget(int row, ItemRole role, QWidget * widget)
int spacing() const
int verticalSpacing() const

Reimplemented Public Functions

virtual void addItem(QLayoutItem * item)
virtual int count() const
virtual Qt::Orientations expandingDirections() const
virtual bool hasHeightForWidth() const
virtual int heightForWidth(int width) const
virtual void invalidate()
virtual QLayoutItem * itemAt(int index) const
virtual QSize minimumSize() const
virtual void setGeometry(const QRect & rect)
virtual QSize sizeHint() const
virtual QLayoutItem * takeAt(int index)
  • 37 public functions inherited from QLayout
  • 31 public functions inherited from QObject
  • 17 public functions inherited from QLayoutItem

Additional Inherited Members

  • 1 public slot inherited from QObject
  • 2 signals inherited from QObject
  • 1 public variable inherited from QObject
  • 1 static public member inherited from QLayout
  • 10 static public members inherited from QObject
  • 4 protected functions inherited from QLayout
  • 9 protected functions inherited from QObject
  • 2 protected variables inherited from QObject

Detailed Description

The QFormLayout class manages forms of input widgets and their associated labels.

QFormLayout is a convenience layout class that lays out its children in a two-column form. The left column consists of labels and the right column consists of "field" widgets (line editors, spin boxes, etc.).

Traditionally, such two-column form layouts were achieved using QGridLayout. QFormLayout is a higher-level alternative that provides the following advantages:

  • Adherence to the different platform's look and feel guidelines.

    For example, the Mac OS X Aqua and KDE guidelines specify that the labels should be right-aligned, whereas Windows and GNOME applications normally use left-alignment.

  • Support for wrapping long rows.

    For devices with small displays, QFormLayout can be set to wrap long rows, or even to wrap all rows.

  • Convenient API for creating label--field pairs.

    The addRow() overload that takes a QString and a QWidget * creates a QLabel behind the scenes and automatically set up its buddy. We can then write code like this:

    QFormLayout *formLayout = new QFormLayout;
    formLayout->addRow(tr("&Name:"), nameLineEdit);
    formLayout->addRow(tr("&Email:"), emailLineEdit);
    formLayout->addRow(tr("&Age:"), ageSpinBox);
    setLayout(formLayout);

    Compare this with the following code, written using QGridLayout:

    nameLabel = new QLabel(tr("&Name:"));
    nameLabel->setBuddy(nameLineEdit); emailLabel = new QLabel(tr("&Name:"));
    emailLabel->setBuddy(emailLineEdit); ageLabel = new QLabel(tr("&Name:"));
    ageLabel->setBuddy(ageSpinBox); QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(nameLabel, 0, 0);
    gridLayout->addWidget(nameLineEdit, 0, 1);
    gridLayout->addWidget(emailLabel, 1, 0);
    gridLayout->addWidget(emailLineEdit, 1, 1);
    gridLayout->addWidget(ageLabel, 2, 0);
    gridLayout->addWidget(ageSpinBox, 2, 1);
    setLayout(gridLayout);

The table below shows the default appearance in different styles.

QCommonStyle derived styles (except QPlastiqueStyle) QMacStyle QPlastiqueStyle Qt Extended styles
Traditional style used for Windows, GNOME, and earlier versions of KDE. Labels are left aligned, and expanding fields grow to fill the available space. (This normally corresponds to what we would get using a two-columnQGridLayout.) Style based on the Mac OS X Aquaguidelines. Labels are right-aligned, the fields don't grow beyond their size hint, and the form is horizontally centered. Recommended style for KDE applications. Similar to MacStyle, except that the form is left-aligned and all fields grow to fill the available space. Default style for Qt Extended styles. Labels are right-aligned, expanding fields grow to fill the available space, and row wrapping is enabled for long lines.

The form styles can be also be overridden individually by calling setLabelAlignment(), setFormAlignment(), setFieldGrowthPolicy(), and setRowWrapPolicy(). For example, to simulate the form layout appearance of QMacStyle on all platforms, but with left-aligned labels, you could write:

formLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);
formLayout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
formLayout->setFormAlignment(Qt::AlignHCenter | Qt::AlignTop);
formLayout->setLabelAlignment(Qt::AlignLeft);

See also QGridLayoutQBoxLayout, and QStackedLayout.

Member Type Documentation

enum QFormLayout::FieldGrowthPolicy

This enum specifies the different policies that can be used to control the way in which the form's fields grow.

Constant Value Description
QFormLayout::FieldsStayAtSizeHint 0 The fields never grow beyond their effective size hint. This is the default for QMacStyle.
QFormLayout::ExpandingFieldsGrow 1 Fields with an horizontal size policy of Expanding or MinimumExpanding will grow to fill the available space. The other fields will not grow beyond their effective size hint. This is the default policy for Plastique.
QFormLayout::AllNonFixedFieldsGrow 2 All fields with a size policy that allows them to grow will grow to fill the available space. This is the default policy for most styles.

See also fieldGrowthPolicy.

enum QFormLayout::ItemRole

This enum specifies the types of widgets (or other layout items) that may appear in a row.

Constant Value Description
QFormLayout::LabelRole 0 A label widget.
QFormLayout::FieldRole 1 A field widget.
QFormLayout::SpanningRole 2 A widget that spans label and field columns.

See also itemAt() and getItemPosition().

enum QFormLayout::RowWrapPolicy

This enum specifies the different policies that can be used to control the way in which the form's rows wrap.

Constant Value Description
QFormLayout::DontWrapRows 0 Fields are always laid out next to their label. This is the default policy for all styles except Qt Extended styles.
QFormLayout::WrapLongRows 1 Labels are given enough horizontal space to fit the widest label, and the rest of the space is given to the fields. If the minimum size of a field pair is wider than the available space, the field is wrapped to the next line. This is the default policy for Qt Extended styles.
QFormLayout::WrapAllRows 2 Fields are always laid out below their label.

See also rowWrapPolicy.

Property Documentation

fieldGrowthPolicy : FieldGrowthPolicy

This property holds the way in which the form's fields grow.

The default value depends on the widget or application style. For QMacStyle, the default is FieldsStayAtSizeHint; for QCommonStyle derived styles (like Plastique and Windows), the default isExpandingFieldsGrow; for Qt Extended styles, the default is AllNonFixedFieldsGrow.

If none of the fields can grow and the form is resized, extra space is distributed according to the current form alignment.

Access functions:

FieldGrowthPolicy fieldGrowthPolicy() const
void setFieldGrowthPolicy(FieldGrowthPolicy policy)

See also formAlignment and rowWrapPolicy.

formAlignment : Qt::Alignment

This property holds the alignment of the form layout's contents within the layout's geometry.

The default value depends on the widget or application style. For QMacStyle, the default is Qt::AlignHCenter | Qt::AlignTop; for the other styles, the default is Qt::AlignLeft | Qt::AlignTop.

Access functions:

Qt::Alignment formAlignment() const
void setFormAlignment(Qt::Alignment alignment)

See also labelAlignment and rowWrapPolicy.

horizontalSpacing : int

This property holds the spacing between widgets that are laid out side by side.

By default, if no value is explicitly set, the layout's horizontal spacing is inherited from the parent layout, or from the style settings for the parent widget.

Access functions:

int horizontalSpacing() const
void setHorizontalSpacing(int spacing)

See also verticalSpacingQStyle::pixelMetric(), and PM_LayoutHorizontalSpacing.

labelAlignment : Qt::Alignment

This property holds the horizontal alignment of the labels.

标签的方向属性:属性值时Qt::Alignment

这个属性值保存的是标签水平方向的方向属性。

The default value depends on the widget or application style. For QCommonStyle derived styles, except for QPlastiqueStyle, the default is Qt::AlignLeft; for the other styles, the default isQt::AlignRight.

Access functions:

Qt::Alignment labelAlignment() const
void setLabelAlignment(Qt::Alignment alignment)

See also formAlignment.

RowWrapPolicy : RowWrapPolicy

This property holds the way in which the form's rows wrap.

每行的换行方式:属性值时QFormLayout中的结构体RowWrapPolicy中的值

这个属性保存了表单布局中每行的换行方式

The default value depends on the widget or application style. For Qt Extended styles, the default is WrapLongRows; for the other styles, the default is DontWrapRows.

If you want to display each label above its associated field (instead of next to it), set this property to WrapAllRows.

Access functions:

RowWrapPolicy rowWrapPolicy() const
void setRowWrapPolicy(RowWrapPolicy policy)

See also fieldGrowthPolicy.

verticalSpacing : int

This property holds the spacing between widgets that are laid out vertically.

By default, if no value is explicitly set, the layout's vertical spacing is inherited from the parent layout, or from the style settings for the parent widget.

Access functions:

int verticalSpacing() const
void setVerticalSpacing(int spacing)

See also horizontalSpacingQStyle::pixelMetric(), and PM_LayoutHorizontalSpacing.

Member Function Documentation

QFormLayout::QFormLayout(QWidget * parent = 0)

Constructs a new form layout with the given parent widget.

See also QWidget::setLayout().

QFormLayout::~QFormLayout()

Destroys the form layout.

void QFormLayout::addItem(QLayoutItem * item) [virtual]

Reimplemented from QLayout::addItem().

void QFormLayout::addRow(QWidget * labelQWidget * field)

Adds a new row to the bottom of this form layout, with the given label and field.

See also insertRow().

void QFormLayout::addRow(QWidget * labelQLayout * field)

This is an overloaded function.

void QFormLayout::addRow(const QString & labelTextQWidget * field)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's buddy.

void QFormLayout::addRow(const QString & labelTextQLayout * field)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text.

void QFormLayout::addRow(QWidget * widget)

This is an overloaded function.

Adds the specified widget at the end of this form layout. The widget spans both columns.

void QFormLayout::addRow(QLayout * layout)

This is an overloaded function.

Adds the specified layout at the end of this form layout. The layout spans both columns.

int QFormLayout::count() const [virtual]

Reimplemented from QLayout::count().

Qt::Orientations QFormLayout::expandingDirections() const [virtual]

Reimplemented from QLayoutItem::expandingDirections().

void QFormLayout::getItemPosition(int index, int * rowPtrItemRole * rolePtr) const

Retrieves the row and role (column) of the item at the specified index. If index is out of bounds, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.

See also itemAt(), count(), getLayoutPosition(), and getWidgetPosition().

void QFormLayout::getLayoutPosition(QLayout * layout, int * rowPtrItemRole * rolePtr) const

Retrieves the row and role (column) of the specified child layout. If layout is not in the form layout, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.

void QFormLayout::getWidgetPosition(QWidget * widget, int * rowPtrItemRole * rolePtr) const

Retrieves the row and role (column) of the specified widget in the layout. If widget is not in the layout, *rowPtr is set to -1; otherwise the row is stored in *rowPtr and the role is stored in *rolePtr.

See also getItemPosition() and itemAt().

bool QFormLayout::hasHeightForWidth() const [virtual]

Reimplemented from QLayoutItem::hasHeightForWidth().

int QFormLayout::heightForWidth(int width) const [virtual]

Reimplemented from QLayoutItem::heightForWidth().

void QFormLayout::insertRow(int rowQWidget * labelQWidget * field)

Inserts a new row at position row in this form layout, with the given label and field. If row is out of bounds, the new row is added at the end.

See also addRow().

void QFormLayout::insertRow(int rowQWidget * labelQLayout * field)

This is an overloaded function.

void QFormLayout::insertRow(int row, const QString & labelTextQWidget * field)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text. The field is set as the new QLabel's buddy.

void QFormLayout::insertRow(int row, const QString & labelTextQLayout * field)

This is an overloaded function.

This overload automatically creates a QLabel behind the scenes with labelText as its text.

void QFormLayout::insertRow(int rowQWidget * widget)

This is an overloaded function.

Inserts the specified widget at position row in this form layout. The widget spans both columns. If row is out of bounds, the widget is added at the end.

void QFormLayout::insertRow(int rowQLayout * layout)

This is an overloaded function.

Inserts the specified layout at position row in this form layout. The layout spans both columns. If row is out of bounds, the widget is added at the end.

void QFormLayout::invalidate() [virtual]

Reimplemented from QLayoutItem::invalidate().

QLayoutItem * QFormLayout::itemAt(int rowItemRole role) const

Returns the layout item in the given row with the specified role (column). Returns 0 if there is no such item.

See also QLayout::itemAt() and setItem().

QLayoutItem * QFormLayout::itemAt(int index) const [virtual]

Reimplemented from QLayout::itemAt().

QWidget * QFormLayout::labelForField(QWidget * field) const

Returns the label associated with the given field.

See also itemAt().

QWidget * QFormLayout::labelForField(QLayout * field) const

This is an overloaded function.

QSize QFormLayout::minimumSize() const [virtual]

Reimplemented from QLayoutItem::minimumSize().

int QFormLayout::rowCount() const

Returns the number of rows in the form.

See also QLayout::count().

void QFormLayout::setGeometry(const QRect & rect) [virtual]

Reimplemented from QLayoutItem::setGeometry().

void QFormLayout::setItem(int rowItemRole roleQLayoutItem * item)

Sets the item in the given row for the given role to item, extending the layout with empty rows if necessary.

If the cell is already occupied, the item is not inserted and an error message is sent to the console. The item spans both columns.

Warning: Do not use this function to add child layouts or child widget items. Use setLayout() or setWidget() instead.

See also setLayout().

void QFormLayout::setLayout(int rowItemRole roleQLayout * layout)

Sets the sub-layout in the given row for the given role to layout, extending the form layout with empty rows if necessary.

If the cell is already occupied, the layout is not inserted and an error message is sent to the console.

Note: For most applications, addRow() or insertRow() should be used instead of setLayout().

See also setWidget().

void QFormLayout::setSpacing(int spacing)

This function sets both the vertical and horizontal spacing to spacing.

See also spacing(), setVerticalSpacing(), and setHorizontalSpacing().

void QFormLayout::setWidget(int rowItemRole roleQWidget * widget)

Sets the widget in the given row for the given role to widget, extending the layout with empty rows if necessary.

If the cell is already occupied, the widget is not inserted and an error message is sent to the console.

Note: For most applications, addRow() or insertRow() should be used instead of setWidget().

See also setLayout().

QSize QFormLayout::sizeHint() const [virtual]

Reimplemented from QLayoutItem::sizeHint().

int QFormLayout::spacing() const

If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it returns -1.

See also setSpacing(), verticalSpacing(), and horizontalSpacing().

QLayoutItem * QFormLayout::takeAt(int index) [virtual]

Reimplemented from QLayout::takeAt().

QFormLayout的更多相关文章

  1. Qt之表单布局(QFormLayout)

    简述 QFormLayout管理输入型控件和关联的标签组成的那些Form表单. QFormLayout是一个方便的布局类,其中的控件以两列的形式被布局在表单中.左列包括标签,右列包含输入控件,例如:Q ...

  2. QHBoxLayout 、QFormLayout 遍历子部件,查找QLineEdit控件

    布局如下: QLineEdit * edit1 = new QLineEdit; QLineEdit * edit2 = new QLineEdit; QLineEdit * edit3 = new ...

  3. Qt And MFC UI Layout

    界面布局 起初,计算机的交互是通过输入的代码进行的, 慢慢的有了图形之后, 就开始了图形界面的交互. 目前来说还有语音交互, 视频交互等多媒体的交互. 不管哪一种交互, 最终在计算机的角度都是信号的输 ...

  4. Qt 设计师手册

    Qt设计师(Qt Designer)是使用Qt部件(Widgets)设计和使用图形用户界面(GUI)的工具.它允许我们以所见即所得的方式构建和定制自己的窗口(Windows)或对话框(Dialogs) ...

  5. 【Qt】2.3 使用Qt设计师来创建对话框

    安装完Qt OpenSource之后,在开始菜单目录下会有这几个东西. 其中[Designer]是用来设计窗口界面的程序.所以现在可以使用它来设计一个对话框.在[Qt Creator]中,[设计]这一 ...

  6. 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)

    原文地址:http://blog.csdn.net/qter_wd007/archive/2010/03/13/5377882.aspx 在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个 ...

  7. 《Linux与Qt程序设计》知识框架

    本文主要是通过一本书来大致了解Qt开发的框架,不对具体内容做详细分析. 1.首先弄清楚概念:定义->以自己的话理解是什么-> 实现的是什么功能->用在哪些地方 2.前面认识到的知识点 ...

  8. 《Qt 实战一二三》

    简介 "我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流",不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的 ...

  9. 读Qt Demo——Basic Layouts Example

    此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...

随机推荐

  1. 「Poetize7」电话线路

    描述 每台电话都有一个独一无二的号码,用一个十位的十进制数字串表示.电话a和b之间能直接通信,当且仅当“a与b之间仅有一个数字不同”,或者“交换a的某 两位上的数字后,a与b相同”.而a.b之间建立通 ...

  2. 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12173  Solved: 4354[Submit][Sta ...

  3. 暴力求解——最大乘积 Maximum Product,UVa 11059

    最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...

  4. entityframework分布式事务中遇到的 “与基础事务管理器的通信失败”的解决方法

    首先是ef的多数据库操作实现事务的方法 public int AddDifferenceDB(userinfo1 user1, userinfo user) { ; using (var test2D ...

  5. Eclipse集成环境中Android SDK下载及更新失败解决方案

    由于公司新项目比较忙,有好长一段时间没碰Android开发咯! 近期闲来在网上下了个开源的应用想拿来自己学习下其中的源码及整体设计,当我把下下来的项目导入Eclipse中时,报如下警告: 原因是我本地 ...

  6. mac下的改装人生——把主硬盘换成ssd

    这两天搞得最多的事情就是我的这两块硬盘,基本上的时间都被他们占用去了,但是最后的结果也是让我很开心--开机瞬秒,程序瞬秒,生活质量瞬间高了很多哈. 关于ssd的各种事情,我的另外一篇博客有讲,算是比较 ...

  7. qemu kvm 虚拟化

    虚拟化: KVM是一个基于Linux内核的虚拟机,属于完全虚拟化.虚拟机监控的实现模型有两类:监控模型(Hypervisor)和宿主机模型(Host-based).由于监控模型需要进行处理器调度,还需 ...

  8. Web项目初始化过程

    在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一个ServletCont ...

  9. dubbo源码分析一:整体分析

    本文作为dubbo源码分析的第一章,先从总体上来分析一下dubbo的代码架构.功能及优缺点,注意,本文只分析说明开源版本提供的代码及功能. 1.dubbo的代码架构:  spring适配层:常规的sp ...

  10. quartz 的学习和使用。

    任务调度器, 定时任务,保存好后会被放入触发器,这些触发器被存入到数据库,调度器线程扫描,如果有待触发的打开锁,拿到job信息,更改trigger信息,释放锁,返回所有的trigger列表,再 按照时 ...