QFormLayout

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

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

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

 #include <QFormLayout>

 QT += widget

成员函数列表

Public Types

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

Properties

  • fieldGrowthPolicy : FieldGrowthPolicy
  • formAlignment : Qt::Alignment
  • horizontalSpacing : int
  • labelAlignment : Qt::Alignment
  • rowWrapPolicy : RowWrapPolicy
  • verticalSpacing : int
  • 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 QGridLayout, QBoxLayout, 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 verticalSpacing, QStyle::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 horizontalSpacing, QStyle::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 * label, QWidget * 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 * label, QLayout * field)

This is an overloaded function.

void QFormLayout::addRow(const QString & labelText, QWidget * 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 & labelText, QLayout * 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 * rowPtr, ItemRole * 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 * rowPtr, ItemRole * 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 * rowPtr, ItemRole * 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 row, QWidget * label, QWidget * 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 row, QWidget * label, QLayout * field)

This is an overloaded function.

void QFormLayout::insertRow(int row, const QString & labelText, QWidget * 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 & labelText, QLayout * field)

This is an overloaded function.

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

void QFormLayout::insertRow(int row, QWidget * 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 row, QLayout * 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 row, ItemRole 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 row, ItemRole role, QLayoutItem * 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 row, ItemRole role, QLayout * 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 row, ItemRole role, QWidget * 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().

时间: 2024-12-17 11:06:41

QFormLayout的相关文章

Qt之表单布局(QFormLayout)

简述 QFormLayout管理输入型控件和关联的标签组成的那些Form表单. QFormLayout是一个方便的布局类,其中的控件以两列的形式被布局在表单中.左列包括标签,右列包含输入控件,例如:QLineEdit.QSpinBox等. 简述 使用 常用接口 总结 使用 我们可以通过addRow(const QString &labelText, QWidget *field)来创建一个带有给定文本的QLabel及QWidget控件行,它们可以自动的设置为伙伴关系. QFormLayout *

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

布局如下: QLineEdit * edit1 = new QLineEdit; QLineEdit * edit2 = new QLineEdit; QLineEdit * edit3 = new QLineEdit; QLineEdit * edit4 = new QLineEdit; QFormLayout * formLayout1 = new QFormLayout; QFormLayout * formLayout2 = new QFormLayout; formLayout1->a

QT开发(二十一)——QT布局管理器

QT开发(二十一)--QT布局管理器 一.布局管理器简介 QT中使用绝对定位的布局方式无法自适应窗口的变化. QT中提供了对界面组件进行布局管理的类,用于对界面组件进行管理,能够自动排列窗口中的界面组件,窗口大小变化后自动更新界面组件的大小. QLayout是QT中布局管理器的抽象基类,通过对QLayout的继承,实现了功能各异且互补的布局管理器. 布局管理器不是界面组件,而是界面组件的定位策略. 任意容器类型的组件都可以指定布局管理器. 同一个布局管理器管理中的组件拥有相同的父组件,在设置布局

Qt布局管理器综合实例

1.布局管理器的综合实例------模拟向导用户界面(Windows平台) -----练习开发一个向导用户界面 @1:在同一界面上展现不同的向导页面 @2:通过上一步和下一步按钮进行切换 @3:不同页面上的元素组件和这些组件排布都不相同 @4:页面中的组件通过布局管理进行排布 (1)通过布局嵌套进行界面设计 @1:上一步和下一步这两个按钮用水平布局管理器QHBoxLayout来进行管理,不同页面上的显示的内容只有按钮不变,所以讲不同页面的内容用栈式布局管理器QStackedLayout进行管理,

Qt版音乐播放器

    Qt版音乐播放器 转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 一.关于Qt 1.1 什么是Qt Qt是一个跨平台应用程序和UI开发框架.使用Qt只需一次性开发应用程序,无需重新编写源代码,便可跨不同桌面和嵌入式操作系统部署这些应用程序. Qt Creator是全新的跨平台Qt IDE,可单独使用,也可与Qt库和开发工具组成一套完整的SDK,其中包括:高级C++代码编辑器,项目和集成管理工具,集成的上下文相关的帮助系统,图形化调试器,代码管理

PyQt5应用与实践

一个典型的GUI应用程序可以抽象为:主界面(菜单栏.工具栏.状态栏.内容区域),二级界面(模态.非模态),信息提示(Tooltip),程序图标等组成.本篇根据作者使用PyQt5编写的一个工具,介绍如何使用PyQt5构建一个典型的GUI应用. 1. 主界面 QMainWindow类提供一个有菜单条.锚接窗口(例如工具条)和一个状态条的主应用程序窗口.主窗口通常用在提供一个大的中央窗口部件(例如文本编辑或者绘制画布)以及周围菜单.工具条和一个状态条.QMainWindow常常被继承,因为这使得封装中

QT 学习笔记概述

以下笔记为在看书和实践的过程中的部分记录总结: 0. 窗口布局 1) 支持绝对布局和布局管理器布局; 2) 绝对布局不够灵活.无法自动调整大小,需要手动编写代码调整: 3) 布局管理器管理布局比较灵活.可自动调整管理的其下部件大小.位置: 4) 布局管理器目前有基本布局QBoxLayout(水平布局QHBoxLayout.垂直QVBoxLayout).栅格布局QGridLayout.表单布局QFormLayout.栈布局QStackedLayout: 5) 还有其他的布局,不过一般用于其他目的,

PyQt5 笔记(01):嵌套布局

PyQt5 有四种布局:水平(QHBoxLayout).竖直(QVBoxLayout).网格(QGridLayout).表单(QFormLayout)在窗体中单一的布局应该不难,但若是比较复杂的布局,一般涉及到布局的嵌套,这就头疼了. 本文的四个知识点:1. 布局不能直接嵌套(如果我错了,欢迎指正!)2. 内层的布局必须先“附着”在一个空 QWidget 上3. 然后把这个“承载”着内层布局的空部件添加至外层布局4. 最后,别忘记把全局布局“附着”到窗体本尊 0. 先看效果图 下面对布局进行分析

Qt3升至Qt4需要注意的几件事项浅谈

公司以前的项目是用Qt3写的,随着时间的推移慢慢显示出Qt3有多方面的限制,因此先公司决定用Qt4来改写这个项目,并为软件添加新功能,在此背景先编写此文章. 先扯一下没用的:gotfocus是获得焦点时触发,Lostfocus是失去焦点的时候触发.比如:新建两个文本框,当点击第一个文本框的时候,则触发第一个文本框的getfocus事件,表示第一个文本框获得了焦点,可以进行操作了.然后鼠标点击第二个文本框的时候,第一个文本框首先触发lostfocus事件,标明它已经失去焦点,无法进行操作.同时第二