Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QGridLayout Class Reference

Inheritance diagram for QGridLayout:

QLayout QObject QLayoutItem

List of all members.

Detailed Description

The QGridLayout class lays out widgets in a grid.

QGridLayout takes the space made available to it (by its parent layout or by the parentWidget()), divides it up into rows and columns, and puts each widget it manages into the correct cell.

Columns and rows behave identically; we will discuss columns, but there are equivalent functions for rows.

Each column has a minimum width and a stretch factor. The minimum width is the greatest of that set using setColumnMinimumWidth() and the minimum width of each widget in that column. The stretch factor is set using setColumnStretch() and determines how much of the available space the column will get over and above its necessary minimum.

Normally, each managed widget or layout is put into a cell of its own using addWidget(). It is also possible for a widget to occupy multiple cells using the row and column spanning overloads of addItem() and addWidget(). If you do this, QGridLayout will guess how to distribute the size over the columns/rows (based on the stretch factors).

To remove a widget from a layout, call remove(). Calling QWidget::hide() on a widget also effectively removes the widget from the layout until QWidget::show() is called.

This illustration shows a fragment of a dialog with a five-column, three-row grid (the grid is shown overlaid in magenta):

A grid layout

Columns 0, 2 and 4 in this dialog fragment are made up of a QLabel, a QLineEdit, and a QListBox. Columns 1 and 3 are placeholders made with setColumnMinimumWidth(). Row 0 consists of three QLabel objects, row 1 of three QLineEdit objects and row 2 of three QListBox objects. We used placeholder columns (1 and 3) to get the right amount of space between the columns.

Note that the columns and rows are not equally wide or tall. If you want two columns to have the same width, you must set their minimum widths and stretch factors to be the same yourself. You do this using setColumnMinimumWidth() and setColumnStretch().

If the QGridLayout is not the top-level layout (i.e. does not manage all of the widget's area and children), you must add it to its parent layout when you create it, but before you do anything with it. The normal way to add a layout is by calling addLayout() on the parent layout.

Once you have added your layout you can start putting widgets and other layouts into the cells of your grid layout using addWidget(), addItem(), and addLayout().

QGridLayout also includes two margin widths: the border and the spacing. The border is the width of the reserved space along each of the QGridLayout's four sides. The spacing is the width of the automatically allocated spacing between neighboring boxes.

The default border and the spacing values are provided by the style. The default margin Qt styles specify is 9 for widgets and 11 for top level windows.

See also:
QBoxLayout, QStackedLayout, {Layout Classes}

Definition at line 38 of file qgridlayout.h.

Public Types

enum  SizeConstraint {
  SetDefaultConstraint, SetNoConstraint, SetMinimumSize, SetFixedSize,
  SetMaximumSize, SetMinAndMaxSize

Public Slots

void deleteLater ()


void destroyed (QObject *=0)

Public Member Functions

bool activate ()
void addItem (QLayoutItem *item, int row, int column, int rowSpan=1, int columnSpan=1, Qt::Alignment=0)
void addLayout (QLayout *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment=0)
void addLayout (QLayout *, int row, int column, Qt::Alignment=0)
void addWidget (QWidget *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment=0)
void addWidget (QWidget *, int row, int column, Qt::Alignment=0)
void addWidget (QWidget *w)
Qt::Alignment alignment () const
bool blockSignals (bool b)
QRect cellRect (int row, int column) const
const QObjectListchildren () const
int columnCount () const
int columnMinimumWidth (int column) const
int columnStretch (int column) const
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
int count () const
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
Qt::Orientations expandingDirections () const
template<typename T>
findChild (const QString &name=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &name=QString()) const
QRect geometry () const
void getItemPosition (int idx, int *row, int *column, int *rowSpan, int *columnSpan)
bool hasHeightForWidth () const
int heightForWidth (int) const
virtual int indexOf (QWidget *) const
bool inherits (const char *classname) const
void installEventFilter (QObject *)
void invalidate ()
bool isEmpty () const
bool isEnabled () const
bool isWidgetType () const
QLayoutItemitemAt (int) const
void killTimer (int id)
QLayoutlayout ()
int margin () const
QSize maximumSize () const
QWidgetmenuBar () const
int minimumHeightForWidth (int) const
QSize minimumSize () const
void moveToThread (QThread *thread)
QString objectName () const
Qt::Corner originCorner () const
QObjectparent () const
QWidgetparentWidget () const
QVariant property (const char *name) const
 QGridLayout ()
 QGridLayout (QWidget *parent)
void removeEventFilter (QObject *)
void removeItem (QLayoutItem *)
void removeWidget (QWidget *w)
int rowCount () const
int rowMinimumHeight (int row) const
int rowStretch (int row) const
void setAlignment (Qt::Alignment a)
bool setAlignment (QLayout *l, Qt::Alignment alignment)
bool setAlignment (QWidget *w, Qt::Alignment alignment)
void setColumnMinimumWidth (int column, int minSize)
void setColumnStretch (int column, int stretch)
void setDefaultPositioning (int n, Qt::Orientation orient)
void setEnabled (bool)
void setGeometry (const QRect &)
void setMargin (int)
void setMenuBar (QWidget *w)
void setObjectName (const QString &name)
void setOriginCorner (Qt::Corner)
void setParent (QObject *)
bool setProperty (const char *name, const QVariant &value)
void setRowMinimumHeight (int row, int minSize)
void setRowStretch (int row, int stretch)
void setSizeConstraint (SizeConstraint)
void setSpacing (int)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
SizeConstraint sizeConstraint () const
QSize sizeHint () const
virtual QSpacerItemspacerItem ()
int spacing () const
int startTimer (int interval)
QLayoutItemtakeAt (int)
QThreadthread () const
int totalHeightForWidth (int w) const
QSize totalMaximumSize () const
QSize totalMinimumSize () const
QSize totalSizeHint () const
void update ()
QObjectUserData * userData (uint id) const
virtual QWidgetwidget ()
 ~QGridLayout ()

Static Public Member Functions

static QSize closestAcceptableSize (const QWidget *w, const QSize &s)
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Member Functions

void addChildLayout (QLayout *l)
void addChildWidget (QWidget *w)
void addItem (QLayoutItem *)
QRect alignmentRect (const QRect &) const
void childEvent (QChildEvent *e)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
int receivers (const char *signal) const
QObjectsender () const
virtual void timerEvent (QTimerEvent *)
void widgetEvent (QEvent *)

Protected Attributes

Qt::Alignment align
QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject


int margin
 the width of the outside border of the layout
QString objectName
 the name of this object
SizeConstraint sizeConstraint
 the resize mode of the layout
int spacing
 the spacing between widgets inside the layout


class QApplication
class QCoreApplication
class QCoreApplicationPrivate
struct QMetaObject
class QThreadData

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index