📄 qgridlayout.cpp
字号:
bool reverse = ((r.bottom() > rect.bottom()) || (r.bottom() == rect.bottom() && ((r.right() > rect.right()) != visualHReversed))); int n = things.size(); for (i = 0; i < n; ++i) { QGridBox *box = things.at(reverse ? n-i-1 : i); int r2 = box->toRow(rr); int c2 = box->toCol(cc); int x = colData.at(box->col).pos; int y = rData.at(box->row).pos; int x2p = colData.at(c2).pos + colData.at(c2).size; // x2+1 int y2p = rData.at(r2).pos + rData.at(r2).size; // y2+1 int w = x2p - x; int h = y2p - y; if (visualHReversed) x = r.left() + r.right() - x - w + 1; if (vReversed) y = r.top() + r.bottom() - y - h + 1; box->setGeometry(QRect(x, y, w, h)); }}QRect QGridLayoutPrivate::cellRect(int row, int col) const{ if (row < 0 || row >= rr || col < 0 || col >= cc) return QRect(); const QVector<QLayoutStruct> *rDataPtr; if (has_hfw && hfwData) rDataPtr = hfwData; else rDataPtr = &rowData; return QRect(colData.at(col).pos, rDataPtr->at(row).pos, colData.at(col).size, rDataPtr->at(row).size);}/*! \class QGridLayout \brief The QGridLayout class lays out widgets in a grid. \ingroup geomanagement \ingroup appearance \mainclass 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): \image gridlayout.png 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 margin() and the spacing(). The margin 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 margin() and spacing() values are provided by the style. The default margin Qt styles specify is 9 for child widgets and 11 for windows. The spacing defaults to the same as the margin width for a top-level layout, or to the same as the parent layout. \sa QBoxLayout, QStackedLayout, {Layout Classes}, {Basic Layouts Example}*//*! Constructs a new QGridLayout with parent widget, \a parent. The layout has one row and one column initially, and will expand when new items are inserted.*/QGridLayout::QGridLayout(QWidget *parent) : QLayout(*new QGridLayoutPrivate, 0, parent){ Q_D(QGridLayout); d->expand(1, 1);}/*! Constructs a new grid layout. You must insert this grid into another layout. You can insert widgets and layouts into this layout at any time, but laying out will not be performed before this is inserted into another layout.*/QGridLayout::QGridLayout() : QLayout(*new QGridLayoutPrivate, 0, 0){ Q_D(QGridLayout); d->expand(1, 1);}#ifdef QT3_SUPPORT/*! \obsolete Constructs a new QGridLayout with \a nRows rows, \a nCols columns and parent widget, \a parent. \a parent may not be 0. The grid layout is called \a name. \a margin is the number of pixels between the edge of the widget and its managed children. \a space is the default number of pixels between cells. If \a space is -1, the value of \a margin is used.*/QGridLayout::QGridLayout(QWidget *parent, int nRows, int nCols, int margin, int space, const char *name) : QLayout(*new QGridLayoutPrivate, 0, parent){ Q_D(QGridLayout); d->expand(nRows, nCols); setMargin(margin); setSpacing(space < 0 ? margin : space); setObjectName(QString::fromAscii(name));}/*! \obsolete Constructs a new grid with \a nRows rows and \a nCols columns. If \a spacing is -1, this QGridLayout inherits its parent's spacing(); otherwise \a spacing is used. The grid layout is called \a name. You must insert this grid into another layout. You can insert widgets and layouts into this layout at any time, but laying out will not be performed before this is inserted into another layout.*/QGridLayout::QGridLayout(QLayout *parentLayout, int nRows, int nCols, int spacing, const char *name) : QLayout(*new QGridLayoutPrivate, parentLayout, 0){ Q_D(QGridLayout); d->expand(nRows, nCols); setSpacing(spacing); setObjectName(QString::fromAscii(name));}/*! \obsolete Constructs a new grid with \a nRows rows and \a nCols columns. If \a spacing is -1, this QGridLayout inherits its parent's spacing(); otherwise \a spacing is used. The grid layout is called \a name. You must insert this grid into another layout. You can insert widgets and layouts into this layout at any time, but laying out will not be performed before this is inserted into another layout.*/QGridLayout::QGridLayout(int nRows, int nCols, int spacing, const char *name) : QLayout(*new QGridLayoutPrivate, 0, 0){ Q_D(QGridLayout); d->expand(nRows, nCols); setSpacing(spacing); setObjectName(QString::fromAscii(name));}#endif/*!\internal (mostly)Sets the positioning mode used by addItem(). If \a orient isQt::Horizontal, this layout is expanded to \a n columns, and itemswill be added columns-first. Otherwise it is expanded to \a n rows anditems will be added rows-first.*/void QGridLayout::setDefaultPositioning(int n, Qt::Orientation orient){ Q_D(QGridLayout); if (orient == Qt::Horizontal) { d->expand(1, n); d->addVertical = false; } else { d->expand(n,1); d->addVertical = true; }}/*! Destroys the grid layout. Geometry management is terminated if this is a top-level grid. The layout's widgets aren't destroyed.*/QGridLayout::~QGridLayout(){ Q_D(QGridLayout); d->deleteAll();}/*! \property QGridLayout::horizontalSpacing \brief the spacing between widgets that are laid out side by side \since 4.3 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. \sa verticalSpacing, QStyle::pixelMetric(), {QStyle::}{PM_LayoutHorizontalSpacing}*/void QGridLayout::setHorizontalSpacing(int spacing){ Q_D(QGridLayout); d->horizontalSpacing = spacing; invalidate();}int QGridLayout::horizontalSpacing() const{ Q_D(const QGridLayout); if (d->horizontalSpacing >= 0) { return d->horizontalSpacing; } else { return qSmartSpacing(this, QStyle::PM_LayoutHorizontalSpacing); }}/*! \property QGridLayout::verticalSpacing \brief the spacing between widgets that are laid out on top of each other \since 4.3 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. \sa horizontalSpacing, QStyle::pixelMetric(), {QStyle::}{PM_LayoutHorizontalSpacing}*/void QGridLayout::setVerticalSpacing(int spacing){ Q_D(QGridLayout); d->verticalSpacing = spacing; invalidate();}int QGridLayout::verticalSpacing() const{ Q_D(const QGridLayout); if (d->verticalSpacing >= 0) { return d->verticalSpacing; } else { return qSmartSpacing(this, QStyle::PM_LayoutVerticalSpacing); }}/*! This function sets both the vertical and horizontal spacing to \a spacing. \sa setVerticalSpacing(), setHorizontalSpacing()*/void QGridLayout::setSpacing(int spacing){ Q_D(QGridLayout); d->horizontalSpacing = d->verticalSpacing = spacing; invalidate();}/*! If the vertical spacing is equal to the horizontal spacing, this function returns that value; otherwise it return -1. \sa setSpacing(), verticalSpacing(), horizontalSpacing()*/int QGridLayout::spacing() const{ int hSpacing = horizontalSpacing(); if (hSpacing == verticalSpacing()) { return hSpacing; } else { return -1; }}/*! Returns the number of rows in this grid.*/int QGridLayout::rowCount() const{ Q_D(const QGridLayout); return d->numRows();}/*! Returns the number of columns in this grid.*/int QGridLayout::columnCount() const{ Q_D(const QGridLayout); return d->numCols();}/*! \reimp*/QSize QGridLayout::sizeHint() const{ Q_D(const QGridLayout); QSize result(d->sizeHint(horizontalSpacing(), verticalSpacing())); int left, top, right, bottom; d->effectiveMargins(&left, &top, &right, &bottom); result += QSize(left + right, top + bottom); return result;}/*! \reimp*/QSize QGridLayout::minimumSize() const{ Q_D(const QGridLayout); QSize result(d->minimumSize(horizontalSpacing(), verticalSpacing())); int left, top, right, bottom; d->effectiveMargins(&left, &top, &right, &bottom); result += QSize(left + right, top + bottom); return result;}/*! \reimp*/QSize QGridLayout::maximumSize() const{ Q_D(const QGridLayout); QSize s = d->maximumSize(horizontalSpacing(), verticalSpacing()); int left, top, right, bottom; d->effectiveMargins(&left, &top, &right, &bottom); s += QSize(left + right, top + bottom); s = s.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX)); if (alignment() & Qt::AlignHorizontal_Mask) s.setWidth(QLAYOUTSIZE_MAX); if (alignment() & Qt::AlignVertical_Mask) s.setHeight(QLAYOUTSIZE_MAX); return s;}/*! \reimp*/bool QGridLayout::hasHeightForWidth() const{ return ((QGridLayout*)this)->d_func()->hasHeightForWidth(horizontalSpacing(), verticalSpacing());}/*! \reimp*/int QGridLayout::heightForWidth(int w) const{ Q_D(const QGridLayout); QGridLayoutPrivate *dat = const_cast<QGridLayoutPrivate *>(d); return dat->heightForWidth(w, horizontalSpacing(), verticalSpacing());}/*! \reimp*/int QGridLayout::minimumHeightForWidth(int w) const{ Q_D(const QGridLayout); QGridLayoutPrivate *dat = const_cast<QGridLayoutPrivate *>(d); return dat->minimumHeightForWidth(w, horizontalSpacing(), verticalSpacing());}#ifdef QT3_SUPPORT/*! \compat Searches for widget \a w in this layout (not including child layouts). If \a w is found, it sets \c{*}\a{row} and \c{*}\a{column} to the row and column that the widget occupies and returns true; otherwise returns false. If the widget spans multiple rows/columns, the top-left cell is returned. Use indexOf() and getItemPosition() instead.*/bool QGridLayout::findWidget(QWidget* w, int *row, int *column){ Q_D(QGridLayout); int index = indexOf(w); if (index < 0) return false; int dummy1, dummy2; d->getItemPosition(index, row, column, &dummy1, &dummy2); return true;}#endif/*! \reimp*/int QGridLayout::count() const{ Q_D(const QGridLayout); return d->count();}/*! \reimp*/QLayoutItem *QGridLayout::itemAt(int index) const{ Q_D(const QGridLayout); return d->itemAt(index);}/*! \reimp*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -