Home · All Classes · Main Classes · Deprecated
Public Member Functions

MGridLayoutPolicy Class Reference

Policy that uses the Qt grid layout engine to provide a grid layout.This class provides a policy which is similar to QGraphicsGridLayout, with the advantage of allowing multiple policies and animation. You can use QGraphicsGridLayout instead to slightly reduce memory overhead if these advantages are not required. More...

Inherits MAbstractLayoutPolicy.

List of all members.

Public Member Functions

 MGridLayoutPolicy (MLayout *layout)
virtual ~MGridLayoutPolicy ()
int rowCount () const
int columnCount () const
void setRowSpacing (int row, qreal spacing)
qreal rowSpacing (int row) const
void setColumnSpacing (int column, qreal spacing)
qreal columnSpacing (int column) const
void setRowAlignment (int row, Qt::Alignment alignment)
Qt::Alignment rowAlignment (int row) const
void setColumnAlignment (int column, Qt::Alignment alignment)
Qt::Alignment columnAlignment (int column) const
QGraphicsLayoutItemitemAt (int row, int col) const
void addItem (QGraphicsLayoutItem *item, int row, int column, int rowSpan, int columnSpan, Qt::Alignment alignment=0)
void addItem (QGraphicsLayoutItem *item, int row, int column, Qt::Alignment alignment=0)
void setRowStretchFactor (int row, int stretch)
int rowStretchFactor (int row) const
void setColumnStretchFactor (int column, int stretch)
int columnStretchFactor (int column) const
void setRowMinimumHeight (int row, qreal height)
qreal rowMinimumHeight (int row) const
void setRowPreferredHeight (int row, qreal height)
qreal rowPreferredHeight (int row) const
void setRowMaximumHeight (int row, qreal height)
qreal rowMaximumHeight (int row) const
void setRowFixedHeight (int row, qreal height)
void setColumnMinimumWidth (int column, qreal width)
qreal columnMinimumWidth (int column) const
void setColumnPreferredWidth (int column, qreal width)
qreal columnPreferredWidth (int column) const
void setColumnMaximumWidth (int column, qreal width)
qreal columnMaximumWidth (int column) const
void setColumnFixedWidth (int column, qreal width)
Qt::Alignment alignment (QGraphicsLayoutItem *item) const
void setAlignment (QGraphicsLayoutItem *item, Qt::Alignment alignment)

Detailed Description

Policy that uses the Qt grid layout engine to provide a grid layout.

This class provides a policy which is similar to QGraphicsGridLayout, with the advantage of allowing multiple policies and animation. You can use QGraphicsGridLayout instead to slightly reduce memory overhead if these advantages are not required.

The following example adds items to the grid layout policy:

    /* Create a MLayout that we set the policy for */
    MLayout *layout = new MLayout;
    MGridLayoutPolicy *policy = new MGridLayoutPolicy(layout);
    policy->setSpacing(10);

    /* Add 7 items to the policy, arranging them into a grid with 3 columns */
    for (int i = 0; i < 7; ++i) {
        MLabel *label = new MLabel(QString("Item %1").arg(i + 1));
        policy->addItem(label, i / 3, i % 3);
        label->setObjectName("item");
        label->setAlignment(Qt::AlignCenter);
    }

The result, with appropriate CSS styling, looks like:

mgridlayoutpolicy.jpg
See also:
DuiGridLayoutPolicy example

Limitations - equally-sized columns

It is difficult to create a grid layout with equally-sized columns, with both MGridLayoutPolicy and QGraphicsGridLayout. The current workaround is to manually set the preferred width of all the items inside the layout to the same width.

As an example, to layout items in two columns with equal widths:

    /* Create a MLayout that we set the policy for */
    MLayout *layout = new MLayout(page.centralWidget());
    MLinearLayoutPolicy *policy = new MLinearLayoutPolicy(layout, Qt::Horizontal);

    /* Setup first layout with a label and text edit */
    MLayout *nameLayout = new MLayout;
    MLinearLayoutPolicy *namePolicy = new MLinearLayoutPolicy(nameLayout, Qt::Horizontal);
    MLabel *textEditLabel = new MLabel("Name:");
    MTextEdit *textEdit = new MTextEdit(MTextEditModel::MultiLine);
    namePolicy->addItem(textEditLabel);  //Add the label and textedit
    namePolicy->addItem(textEdit);
    textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    /* Setup second layout with a large label */
    MLayout *labelLayout = new MLayout;
    MLinearLayoutPolicy *labelPolicy = new MLinearLayoutPolicy(labelLayout, Qt::Horizontal);
    MLabel *label = new MLabel("Enter the name of the person who likes to listen to music while sorting their socks!");
    label->setObjectName("nameLabel");
    labelPolicy->addItem(label);
    label->setWordWrap(true);

    /* Add the two layouts to the layout */
    policy->addItem(nameLayout);
    policy->addItem(labelLayout);

    /* Make the two layouts have an equal preferred size, so that they get an equal amount of space */
    nameLayout->setPreferredWidth(1);
    labelLayout->setPreferredWidth(1);

We are working with Qt to try to find a better solution.

Using QGraphicsGridLayout instead

If you do not need animations or multiple policies, you can use QGraphicsGridLayout for same effect in less code. For example:

    /* Create a grid layout */
    QGraphicsGridLayout *layout = new QGraphicsGridLayout;
    layout->setSpacing(10);

    /* Add 7 items to the layout, arranging them into a grid with 3 columns */
    for (int i = 0; i < 7; ++i) {
        MLabel *label = new MLabel(QString("Item %1").arg(i + 1));
        layout->addItem(label, i / 3, i % 3);
        label->setObjectName("item");
        label->setAlignment(Qt::AlignCenter);
    }

See also:
QGraphicsGridLayout example

Constructor & Destructor Documentation

MGridLayoutPolicy::MGridLayoutPolicy ( MLayout layout  )  [explicit]

Constructs a grid layout policy.

Parameters:
layout The layout to associate with.
MGridLayoutPolicy::~MGridLayoutPolicy (  )  [virtual]

Destructor to clean up engine.


Member Function Documentation

void MGridLayoutPolicy::addItem ( QGraphicsLayoutItem item,
int  row,
int  column,
int  rowSpan,
int  columnSpan,
Qt::Alignment  alignment = 0 
)

Add an item at a position in the grid.

Parameters:
item The item to add.
row The row to add the item to.
column The column to add the item to.
rowSpan The number of cells the item spans in the row
columnSpan The number of cells the item spans in the column
alignment The alignment to use.
void MGridLayoutPolicy::addItem ( QGraphicsLayoutItem item,
int  row,
int  column,
Qt::Alignment  alignment = 0 
) [inline]

Add an item at a position in the grid.

Parameters:
item The item to add.
row The row to add the item to.
column The column to add the item to.
alignment The alignment to use.
Qt::Alignment MGridLayoutPolicy::alignment ( QGraphicsLayoutItem item  )  const

Returns the alignment for item. The default alignment is Qt::AlignCenter.

The alignment decides how the item is positioned within its assigned space in the case where there's more space available in the layout than the widgets can occupy.

See also:
setAlignment()
Qt::Alignment MGridLayoutPolicy::columnAlignment ( int  column  )  const

Get the default alignment for a whole column.

Parameters:
column the row to get the alignment for
Returns:
the default alignment in use
int MGridLayoutPolicy::columnCount (  )  const

Get the number of columns.

Returns:
The number of columns.
qreal MGridLayoutPolicy::columnMaximumWidth ( int  column  )  const

Returns the maximum width for column.

qreal MGridLayoutPolicy::columnMinimumWidth ( int  column  )  const

Returns the minimum width for column.

qreal MGridLayoutPolicy::columnPreferredWidth ( int  column  )  const

Returns the preferred width for column.

qreal MGridLayoutPolicy::columnSpacing ( int  column  )  const

Get spacing for a single column.

Parameters:
column The column to get the spacing for.
Returns:
The spacing.
int MGridLayoutPolicy::columnStretchFactor ( int  column  )  const

Returns the stretch factor for column.

QGraphicsLayoutItem * MGridLayoutPolicy::itemAt ( int  row,
int  col 
) const

Get an item.

Get the item found at the given position in the grid.

Parameters:
row The row to query.
col The column to query.
Returns:
The item at the position or 0 if invalid/unset.
Qt::Alignment MGridLayoutPolicy::rowAlignment ( int  row  )  const

Get the default alignment for a whole row.

Parameters:
row the row to get the alignment for
Returns:
the default alignment in use
int MGridLayoutPolicy::rowCount (  )  const

Get the number of rows.

Returns:
The number of rows.
qreal MGridLayoutPolicy::rowMaximumHeight ( int  row  )  const

Returns the maximum height for row.

qreal MGridLayoutPolicy::rowMinimumHeight ( int  row  )  const

Returns the minimum height for row.

qreal MGridLayoutPolicy::rowPreferredHeight ( int  row  )  const

Returns the preferred height for row.

qreal MGridLayoutPolicy::rowSpacing ( int  row  )  const

Get spacing for a single row.

Parameters:
row The row to get the spacing for.
Returns:
The spacing.
int MGridLayoutPolicy::rowStretchFactor ( int  row  )  const

Returns the stretch factor for row.

void MGridLayoutPolicy::setAlignment ( QGraphicsLayoutItem item,
Qt::Alignment  alignment 
)

Sets the alignment of item to alignment. If item's alignment changes, the layout is automatically invalidated.

See also:
alignment(), invalidate()
void MGridLayoutPolicy::setColumnAlignment ( int  column,
Qt::Alignment  alignment 
)

Set the default alignment for a whole column. This value can be overridden on an item for item basis.

Parameters:
column the column to set the default alignment for
alignment the default alignment to use
void MGridLayoutPolicy::setColumnFixedWidth ( int  column,
qreal  width 
)

Sets the fixed width of column to width.

void MGridLayoutPolicy::setColumnMaximumWidth ( int  column,
qreal  width 
)

Sets the maximum width of column to width.

void MGridLayoutPolicy::setColumnMinimumWidth ( int  column,
qreal  width 
)

Sets the minimum width for column to width.

void MGridLayoutPolicy::setColumnPreferredWidth ( int  column,
qreal  width 
)

Sets the preferred width for column to width.

void MGridLayoutPolicy::setColumnSpacing ( int  column,
qreal  spacing 
)

Set spacing for a single column.

Parameters:
column The column to set the spacing for.
spacing The spacing to use.
void MGridLayoutPolicy::setColumnStretchFactor ( int  column,
int  stretch 
)

Sets the stretch factor for column.

void MGridLayoutPolicy::setRowAlignment ( int  row,
Qt::Alignment  alignment 
)

Set the default alignment for a whole row. This value can be overridden on an item for item basis.

Parameters:
row the row to set the default alignment for
alignment the default alignment to use
void MGridLayoutPolicy::setRowFixedHeight ( int  row,
qreal  height 
)

Sets the fixed height for row to height.

void MGridLayoutPolicy::setRowMaximumHeight ( int  row,
qreal  height 
)

Sets the maximum height for row to height.

void MGridLayoutPolicy::setRowMinimumHeight ( int  row,
qreal  height 
)

Sets the minimum height for row to height.

void MGridLayoutPolicy::setRowPreferredHeight ( int  row,
qreal  height 
)

Sets the preferred height for row.

void MGridLayoutPolicy::setRowSpacing ( int  row,
qreal  spacing 
)

Set spacing for a single row.

Parameters:
row The row to set the spacing for.
spacing The spacing to use.
void MGridLayoutPolicy::setRowStretchFactor ( int  row,
int  stretch 
)

Sets the stretch factor for row.


Copyright © 2010 Nokia Corporation
MeeGo Touch