📄 qgridlayout.cpp
字号:
We must increase the maximum size of at least one of the items. qGeomCalc() will put the extra space in between the items. We must recover that extra space and put it somewhere. It does not really matter where, since the user can always specify stretch factors and avoid this code. */ qGeomCalc(chain, start, end - start + 1, 0, minSize, spacing); int pos = 0; for (i = start; i <= end; i++) { int nextPos = (i == end) ? minSize - 1 : chain[i + 1].pos; int realSize = nextPos - pos; if (i != end) realSize -= spacing; if (chain[i].minimumSize < realSize) chain[i].minimumSize = realSize; if (chain[i].maximumSize < chain[i].minimumSize) chain[i].maximumSize = chain[i].minimumSize; pos = nextPos; } } else if (w < minSize) { qGeomCalc(chain, start, end - start + 1, 0, minSize, spacing); for (i = start; i <= end; i++) { if (chain[i].minimumSize < chain[i].size) chain[i].minimumSize = chain[i].size; } } if (wh < sizeHint) { qGeomCalc(chain, start, end - start + 1, 0, sizeHint, spacing); for (i = start; i <= end; i++) { if (chain[i].sizeHint < chain[i].size) chain[i].sizeHint = chain[i].size; } }}//#define QT_LAYOUT_DISABLE_CACHINGvoid QGridLayoutPrivate::setupLayoutData(int spacing){#ifndef QT_LAYOUT_DISABLE_CACHING if (!needRecalc) return;#endif has_hfw = false; int i; for (i = 0; i < rr; i++) rowData[i].init(rStretch[i], rSpacing[i]); for (i = 0; i < cc; i++) colData[i].init(cStretch[i], cSpacing[i]); for (int pass = 0; pass < 2; ++pass) { for (i = 0; i < things.size(); ++i) { QGridBox *box = things.at(i); int r1 = box->row; int c1 = box->col; int r2 = box->torow; int c2 = box->tocol; if (r2 < 0) r2 = rr - 1; if (c2 < 0) c2 = cc - 1; QSize hint = box->sizeHint(); QSize min = box->minimumSize(); if (box->hasHeightForWidth()) has_hfw = true; if (r1 == r2) { if (pass == 0) addData(box, true, false); } else { if (pass == 1) distributeMultiBox(rowData, spacing, r1, r2, min.height(), hint.height(), rStretch, box->vStretch()); } if (c1 == c2) { if (pass == 0) addData(box, false, true); } else { if (pass == 1) distributeMultiBox(colData, spacing, c1, c2, min.width(), hint.width(), cStretch, box->hStretch()); } } } for (i = 0; i < rr; i++) rowData[i].expansive = rowData[i].expansive || rowData[i].stretch > 0; for (i = 0; i < cc; i++) colData[i].expansive = colData[i].expansive || colData[i].stretch > 0; needRecalc = false;}void QGridLayoutPrivate::addHfwData(QGridBox *box, int width){ QVector<QLayoutStruct> &rData = *hfwData; if (box->hasHeightForWidth()) { int hint = box->heightForWidth(width); rData[box->row].sizeHint = qMax(hint, rData[box->row].sizeHint); rData[box->row].minimumSize = qMax(hint, rData[box->row].minimumSize); } else { QSize hint = box->sizeHint(); QSize minS = box->minimumSize(); rData[box->row].sizeHint = qMax(hint.height(), rData[box->row].sizeHint); rData[box->row].minimumSize = qMax(minS.height(), rData[box->row].minimumSize); }}/* Similar to setupLayoutData(), but uses heightForWidth(colData) instead of sizeHint(). Assumes that setupLayoutData() and qGeomCalc(colData) has been called.*/void QGridLayoutPrivate::setupHfwLayoutData(int spacing){ QVector<QLayoutStruct> &rData = *hfwData; for (int i = 0; i < rr; i++) { rData[i] = rowData[i]; rData[i].minimumSize = rData[i].sizeHint = rSpacing[i]; } for (int pass = 0; pass < 2; ++pass) { for (int i = 0; i < things.size(); ++i) { QGridBox *box = things.at(i); int r1 = box->row; int c1 = box->col; int r2 = box->torow; int c2 = box->tocol; if (r2 < 0) r2 = rr-1; if (c2 < 0) c2 = cc-1; int w = colData[c2].pos + colData[c2].size - colData[c1].pos; if (r1 == r2) { if (pass == 0) addHfwData(box, w); } else { if (pass == 1) { QSize hint = box->sizeHint(); QSize min = box->minimumSize(); if (box->hasHeightForWidth()) { int hfwh = box->heightForWidth(w); if (hfwh > hint.height()) hint.setHeight(hfwh); if (hfwh > min.height()) min.setHeight(hfwh); } distributeMultiBox(rData, spacing, r1, r2, min.height(), hint.height(), rStretch, box->vStretch()); } } } } for (int i = 0; i < rr; i++) rData[i].expansive = rData[i].expansive || rData[i].stretch > 0;}void QGridLayoutPrivate::distribute(QRect r, int spacing){ Q_Q(QGridLayout); bool visualHReversed = hReversed; QWidget *parent = q->parentWidget(); if (parent && parent->isRightToLeft()) visualHReversed = !visualHReversed; setupLayoutData(spacing); qGeomCalc(colData, 0, cc, r.x(), r.width(), spacing); QVector<QLayoutStruct> *rDataPtr; if (has_hfw) { recalcHFW(r.width(), spacing); qGeomCalc(*hfwData, 0, rr, r.y(), r.height(), spacing); rDataPtr = hfwData; } else { qGeomCalc(rowData, 0, rr, r.y(), r.height(), spacing); rDataPtr = &rowData; } QVector<QLayoutStruct> &rData = *rDataPtr; int i; 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; int c2 = box->tocol; if (r2 < 0) r2 = rr-1; if (c2 < 0) c2 = cc-1; int x = colData[box->col].pos; int y = rData[box->row].pos; int x2p = colData[c2].pos + colData[c2].size; // x2+1 int y2p = rData[r2].pos + rData[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) rDataPtr = hfwData; else rDataPtr = &rowData; return QRect(colData[col].pos, (*rDataPtr)[row].pos, colData[col].size, (*rDataPtr)[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 \l margin and the \l 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 \l margin and \l 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}*//*! 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();}/*! 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); int m = margin(); return d->sizeHint(spacing()) + QSize(2 * m, 2 * m);}/*! \reimp*/QSize QGridLayout::minimumSize() const{ Q_D(const QGridLayout); int m = margin(); return d->minimumSize(spacing()) + QSize(2 * m, 2 * m);}/*! \reimp*/QSize QGridLayout::maximumSize() const{ Q_D(const QGridLayout); int m = margin(); QSize s = d->maximumSize(spacing()) + QSize(2 * m, 2 * m); 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(spacing());}/*! \reimp*/int QGridLayout::heightForWidth(int w) const{ QGridLayout *that = (QGridLayout*)this; return that->d_func()->heightForWidth(w, margin(), spacing());}/*! \reimp*/int QGridLayout::minimumHeightForWidth(int w) const{ QGridLayout *that = (QGridLayout*)this; return that->d_func()->minimumHeightForWidth(w, margin(), spacing());}#ifdef QT3_SUPPORT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -