| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qboxlayout.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count |
|---|---|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
| 4 | ** Contact: https://www.qt.io/licensing/ | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 3 as published by the Free Software | - |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
| 21 | ** packaging of this file. Please review the following information to | - |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
| 24 | ** | - |
| 25 | ** GNU General Public License Usage | - |
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - |
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
| 31 | ** included in the packaging of this file. Please review the following | - |
| 32 | ** information to ensure the GNU General Public License requirements will | - |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
| 35 | ** | - |
| 36 | ** $QT_END_LICENSE$ | - |
| 37 | ** | - |
| 38 | ****************************************************************************/ | - |
| 39 | - | |
| 40 | #include "qboxlayout.h" | - |
| 41 | #include "qapplication.h" | - |
| 42 | #include "qwidget.h" | - |
| 43 | #include "qlist.h" | - |
| 44 | #include "qsizepolicy.h" | - |
| 45 | #include "qvector.h" | - |
| 46 | - | |
| 47 | #include "qlayoutengine_p.h" | - |
| 48 | #include "qlayout_p.h" | - |
| 49 | - | |
| 50 | QT_BEGIN_NAMESPACE | - |
| 51 | - | |
| 52 | struct QBoxLayoutItem | - |
| 53 | { | - |
| 54 | QBoxLayoutItem(QLayoutItem *it, int stretch_ = 0) | - |
| 55 | : item(it), stretch(stretch_), magic(false) { } | - |
| 56 | ~QBoxLayoutItem() { delete item; } | - |
| 57 | - | |
| 58 | int hfw(int w) { | - |
| 59 | if (item->hasHeightForWidth()) { | - |
| 60 | return item->heightForWidth(w); | - |
| 61 | } else { | - |
| 62 | return item->sizeHint().height(); | - |
| 63 | } | - |
| 64 | } | - |
| 65 | int mhfw(int w) { | - |
| 66 | if (item->hasHeightForWidth()) { | - |
| 67 | return item->heightForWidth(w); | - |
| 68 | } else { | - |
| 69 | return item->minimumSize().height(); | - |
| 70 | } | - |
| 71 | } | - |
| 72 | int hStretch() { | - |
| 73 | if (stretch == 0 && item->widget()) { | - |
| 74 | return item->widget()->sizePolicy().horizontalStretch(); | - |
| 75 | } else { | - |
| 76 | return stretch; | - |
| 77 | } | - |
| 78 | } | - |
| 79 | int vStretch() { | - |
| 80 | if (stretch == 0 && item->widget()) { | - |
| 81 | return item->widget()->sizePolicy().verticalStretch(); | - |
| 82 | } else { | - |
| 83 | return stretch; | - |
| 84 | } | - |
| 85 | } | - |
| 86 | - | |
| 87 | QLayoutItem *item; | - |
| 88 | int stretch; | - |
| 89 | bool magic; | - |
| 90 | }; | - |
| 91 | - | |
| 92 | class QBoxLayoutPrivate : public QLayoutPrivate | - |
| 93 | { | - |
| 94 | Q_DECLARE_PUBLIC(QBoxLayout) | - |
| 95 | public: | - |
| 96 | QBoxLayoutPrivate() : hfwWidth(-1), dirty(true), spacing(-1) { } | - |
| 97 | ~QBoxLayoutPrivate(); | - |
| 98 | - | |
| 99 | void setDirty() { | - |
| 100 | geomArray.clear(); | - |
| 101 | hfwWidth = -1; | - |
| 102 | hfwHeight = -1; | - |
| 103 | dirty = true; | - |
| 104 | } | - |
| 105 | - | |
| 106 | QList<QBoxLayoutItem *> list; | - |
| 107 | QVector<QLayoutStruct> geomArray; | - |
| 108 | int hfwWidth; | - |
| 109 | int hfwHeight; | - |
| 110 | int hfwMinHeight; | - |
| 111 | QSize sizeHint; | - |
| 112 | QSize minSize; | - |
| 113 | QSize maxSize; | - |
| 114 | int leftMargin, topMargin, rightMargin, bottomMargin; | - |
| 115 | Qt::Orientations expanding; | - |
| 116 | uint hasHfw : 1; | - |
| 117 | uint dirty : 1; | - |
| 118 | QBoxLayout::Direction dir; | - |
| 119 | int spacing; | - |
| 120 | - | |
| 121 | inline void deleteAll() { while (!list.isEmpty()) delete list.takeFirst(); } | - |
| 122 | - | |
| 123 | void setupGeom(); | - |
| 124 | void calcHfw(int); | - |
| 125 | - | |
| 126 | void effectiveMargins(int *left, int *top, int *right, int *bottom) const; | - |
| 127 | QLayoutItem* replaceAt(int index, QLayoutItem*) Q_DECL_OVERRIDE; | - |
| 128 | }; | - |
| 129 | - | |
| 130 | QBoxLayoutPrivate::~QBoxLayoutPrivate() | - |
| 131 | { | - |
| 132 | } | - |
| 133 | - | |
| 134 | static inline bool horz(QBoxLayout::Direction dir) | - |
| 135 | { | - |
| 136 | return dir == QBoxLayout::RightToLeft || dir == QBoxLayout::LeftToRight; | - |
| 137 | } | - |
| 138 | - | |
| 139 | /** | - |
| 140 | * The purpose of this function is to make sure that widgets are not laid out outside its layout. | - |
| 141 | * E.g. the layoutItemRect margins are only meant to take of the surrounding margins/spacings. | - |
| 142 | * However, if the margin is 0, it can easily cover the area of a widget above it. | - |
| 143 | */ | - |
| 144 | void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *bottom) const | - |
| 145 | { | - |
| 146 | int l = leftMargin; | - |
| 147 | int t = topMargin; | - |
| 148 | int r = rightMargin; | - |
| 149 | int b = bottomMargin; | - |
| 150 | #ifdef Q_OS_MAC | - |
| 151 | Q_Q(const QBoxLayout); | - |
| 152 | if (horz(dir)) { | - |
| 153 | QBoxLayoutItem *leftBox = 0; | - |
| 154 | QBoxLayoutItem *rightBox = 0; | - |
| 155 | - | |
| 156 | if (left || right) { | - |
| 157 | leftBox = list.value(0); | - |
| 158 | rightBox = list.value(list.count() - 1); | - |
| 159 | if (dir == QBoxLayout::RightToLeft) | - |
| 160 | qSwap(leftBox, rightBox); | - |
| 161 | - | |
| 162 | int leftDelta = 0; | - |
| 163 | int rightDelta = 0; | - |
| 164 | if (leftBox) { | - |
| 165 | QLayoutItem *itm = leftBox->item; | - |
| 166 | if (QWidget *w = itm->widget()) | - |
| 167 | leftDelta = itm->geometry().left() - w->geometry().left(); | - |
| 168 | } | - |
| 169 | if (rightBox) { | - |
| 170 | QLayoutItem *itm = rightBox->item; | - |
| 171 | if (QWidget *w = itm->widget()) | - |
| 172 | rightDelta = w->geometry().right() - itm->geometry().right(); | - |
| 173 | } | - |
| 174 | QWidget *w = q->parentWidget(); | - |
| 175 | Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QApplication::layoutDirection(); | - |
| 176 | if (layoutDirection == Qt::RightToLeft) | - |
| 177 | qSwap(leftDelta, rightDelta); | - |
| 178 | - | |
| 179 | l = qMax(l, leftDelta); | - |
| 180 | r = qMax(r, rightDelta); | - |
| 181 | } | - |
| 182 | - | |
| 183 | int count = top || bottom ? list.count() : 0; | - |
| 184 | for (int i = 0; i < count; ++i) { | - |
| 185 | QBoxLayoutItem *box = list.at(i); | - |
| 186 | QLayoutItem *itm = box->item; | - |
| 187 | QWidget *w = itm->widget(); | - |
| 188 | if (w) { | - |
| 189 | QRect lir = itm->geometry(); | - |
| 190 | QRect wr = w->geometry(); | - |
| 191 | if (top) | - |
| 192 | t = qMax(t, lir.top() - wr.top()); | - |
| 193 | if (bottom) | - |
| 194 | b = qMax(b, wr.bottom() - lir.bottom()); | - |
| 195 | } | - |
| 196 | } | - |
| 197 | } else { // vertical layout | - |
| 198 | QBoxLayoutItem *topBox = 0; | - |
| 199 | QBoxLayoutItem *bottomBox = 0; | - |
| 200 | - | |
| 201 | if (top || bottom) { | - |
| 202 | topBox = list.value(0); | - |
| 203 | bottomBox = list.value(list.count() - 1); | - |
| 204 | if (dir == QBoxLayout::BottomToTop) { | - |
| 205 | qSwap(topBox, bottomBox); | - |
| 206 | } | - |
| 207 | - | |
| 208 | if (top && topBox) { | - |
| 209 | QLayoutItem *itm = topBox->item; | - |
| 210 | QWidget *w = itm->widget(); | - |
| 211 | if (w) | - |
| 212 | t = qMax(t, itm->geometry().top() - w->geometry().top()); | - |
| 213 | } | - |
| 214 | - | |
| 215 | if (bottom && bottomBox) { | - |
| 216 | QLayoutItem *itm = bottomBox->item; | - |
| 217 | QWidget *w = itm->widget(); | - |
| 218 | if (w) | - |
| 219 | b = qMax(b, w->geometry().bottom() - itm->geometry().bottom()); | - |
| 220 | } | - |
| 221 | } | - |
| 222 | - | |
| 223 | int count = left || right ? list.count() : 0; | - |
| 224 | for (int i = 0; i < count; ++i) { | - |
| 225 | QBoxLayoutItem *box = list.at(i); | - |
| 226 | QLayoutItem *itm = box->item; | - |
| 227 | QWidget *w = itm->widget(); | - |
| 228 | if (w) { | - |
| 229 | QRect lir = itm->geometry(); | - |
| 230 | QRect wr = w->geometry(); | - |
| 231 | if (left) | - |
| 232 | l = qMax(l, lir.left() - wr.left()); | - |
| 233 | if (right) | - |
| 234 | r = qMax(r, wr.right() - lir.right()); | - |
| 235 | } | - |
| 236 | } | - |
| 237 | } | - |
| 238 | #endif | - |
| 239 | if (left) | - |
| 240 | *left = l; | - |
| 241 | if (top) | - |
| 242 | *top = t; | - |
| 243 | if (right) | - |
| 244 | *right = r; | - |
| 245 | if (bottom) | - |
| 246 | *bottom = b; | - |
| 247 | } | - |
| 248 | - | |
| 249 | - | |
| 250 | /* | - |
| 251 | Initializes the data structure needed by qGeomCalc and | - |
| 252 | recalculates max/min and size hint. | - |
| 253 | */ | - |
| 254 | void QBoxLayoutPrivate::setupGeom() | - |
| 255 | { | - |
| 256 | if (!dirty) | - |
| 257 | return; | - |
| 258 | - | |
| 259 | Q_Q(QBoxLayout); | - |
| 260 | int maxw = horz(dir) ? 0 : QLAYOUTSIZE_MAX; | - |
| 261 | int maxh = horz(dir) ? QLAYOUTSIZE_MAX : 0; | - |
| 262 | int minw = 0; | - |
| 263 | int minh = 0; | - |
| 264 | int hintw = 0; | - |
| 265 | int hinth = 0; | - |
| 266 | - | |
| 267 | bool horexp = false; | - |
| 268 | bool verexp = false; | - |
| 269 | - | |
| 270 | hasHfw = false; | - |
| 271 | - | |
| 272 | int n = list.count(); | - |
| 273 | geomArray.clear(); | - |
| 274 | QVector<QLayoutStruct> a(n); | - |
| 275 | - | |
| 276 | QSizePolicy::ControlTypes controlTypes1; | - |
| 277 | QSizePolicy::ControlTypes controlTypes2; | - |
| 278 | int fixedSpacing = q->spacing(); | - |
| 279 | int previousNonEmptyIndex = -1; | - |
| 280 | - | |
| 281 | QStyle *style = 0; | - |
| 282 | if (fixedSpacing < 0) { | - |
| 283 | if (QWidget *parentWidget = q->parentWidget()) | - |
| 284 | style = parentWidget->style(); | - |
| 285 | } | - |
| 286 | - | |
| 287 | for (int i = 0; i < n; i++) { | - |
| 288 | QBoxLayoutItem *box = list.at(i); | - |
| 289 | QSize max = box->item->maximumSize(); | - |
| 290 | QSize min = box->item->minimumSize(); | - |
| 291 | QSize hint = box->item->sizeHint(); | - |
| 292 | Qt::Orientations exp = box->item->expandingDirections(); | - |
| 293 | bool empty = box->item->isEmpty(); | - |
| 294 | int spacing = 0; | - |
| 295 | - | |
| 296 | if (!empty) { | - |
| 297 | if (fixedSpacing >= 0) { | - |
| 298 | spacing = (previousNonEmptyIndex >= 0) ? fixedSpacing : 0; | - |
| 299 | #ifdef Q_OS_MAC | - |
| 300 | if (!horz(dir) && previousNonEmptyIndex >= 0) { | - |
| 301 | QBoxLayoutItem *sibling = (dir == QBoxLayout::TopToBottom ? box : list.at(previousNonEmptyIndex)); | - |
| 302 | if (sibling) { | - |
| 303 | QWidget *wid = sibling->item->widget(); | - |
| 304 | if (wid) | - |
| 305 | spacing = qMax(spacing, sibling->item->geometry().top() - wid->geometry().top()); | - |
| 306 | } | - |
| 307 | } | - |
| 308 | #endif | - |
| 309 | } else { | - |
| 310 | controlTypes1 = controlTypes2; | - |
| 311 | controlTypes2 = box->item->controlTypes(); | - |
| 312 | if (previousNonEmptyIndex >= 0) { | - |
| 313 | QSizePolicy::ControlTypes actual1 = controlTypes1; | - |
| 314 | QSizePolicy::ControlTypes actual2 = controlTypes2; | - |
| 315 | if (dir == QBoxLayout::RightToLeft || dir == QBoxLayout::BottomToTop) | - |
| 316 | qSwap(actual1, actual2); | - |
| 317 | - | |
| 318 | if (style) { | - |
| 319 | spacing = style->combinedLayoutSpacing(actual1, actual2, | - |
| 320 | horz(dir) ? Qt::Horizontal : Qt::Vertical, | - |
| 321 | 0, q->parentWidget()); | - |
| 322 | if (spacing < 0) | - |
| 323 | spacing = 0; | - |
| 324 | } | - |
| 325 | } | - |
| 326 | } | - |
| 327 | - | |
| 328 | if (previousNonEmptyIndex >= 0) | - |
| 329 | a[previousNonEmptyIndex].spacing = spacing; | - |
| 330 | previousNonEmptyIndex = i; | - |
| 331 | } | - |
| 332 | - | |
| 333 | bool ignore = empty && box->item->widget(); // ignore hidden widgets | - |
| 334 | bool dummy = true; | - |
| 335 | if (horz(dir)) { | - |
| 336 | bool expand = (exp & Qt::Horizontal || box->stretch > 0); | - |
| 337 | horexp = horexp || expand; | - |
| 338 | maxw += spacing + max.width(); | - |
| 339 | minw += spacing + min.width(); | - |
| 340 | hintw += spacing + hint.width(); | - |
| 341 | if (!ignore) | - |
| 342 | qMaxExpCalc(maxh, verexp, dummy, | - |
| 343 | max.height(), exp & Qt::Vertical, box->item->isEmpty()); | - |
| 344 | minh = qMax(minh, min.height()); | - |
| 345 | hinth = qMax(hinth, hint.height()); | - |
| 346 | - | |
| 347 | a[i].sizeHint = hint.width(); | - |
| 348 | a[i].maximumSize = max.width(); | - |
| 349 | a[i].minimumSize = min.width(); | - |
| 350 | a[i].expansive = expand; | - |
| 351 | a[i].stretch = box->stretch ? box->stretch : box->hStretch(); | - |
| 352 | } else { | - |
| 353 | bool expand = (exp & Qt::Vertical || box->stretch > 0); | - |
| 354 | verexp = verexp || expand; | - |
| 355 | maxh += spacing + max.height(); | - |
| 356 | minh += spacing + min.height(); | - |
| 357 | hinth += spacing + hint.height(); | - |
| 358 | if (!ignore) | - |
| 359 | qMaxExpCalc(maxw, horexp, dummy, | - |
| 360 | max.width(), exp & Qt::Horizontal, box->item->isEmpty()); | - |
| 361 | minw = qMax(minw, min.width()); | - |
| 362 | hintw = qMax(hintw, hint.width()); | - |
| 363 | - | |
| 364 | a[i].sizeHint = hint.height(); | - |
| 365 | a[i].maximumSize = max.height(); | - |
| 366 | a[i].minimumSize = min.height(); | - |
| 367 | a[i].expansive = expand; | - |
| 368 | a[i].stretch = box->stretch ? box->stretch : box->vStretch(); | - |
| 369 | } | - |
| 370 | - | |
| 371 | a[i].empty = empty; | - |
| 372 | a[i].spacing = 0; // might be initialized with a non-zero value in a later iteration | - |
| 373 | hasHfw = hasHfw || box->item->hasHeightForWidth(); | - |
| 374 | } | - |
| 375 | - | |
| 376 | geomArray = a; | - |
| 377 | - | |
| 378 | expanding = (Qt::Orientations) | - |
| 379 | ((horexp ? Qt::Horizontal : 0) | - |
| 380 | | (verexp ? Qt::Vertical : 0)); | - |
| 381 | - | |
| 382 | minSize = QSize(minw, minh); | - |
| 383 | maxSize = QSize(maxw, maxh).expandedTo(minSize); | - |
| 384 | sizeHint = QSize(hintw, hinth).expandedTo(minSize).boundedTo(maxSize); | - |
| 385 | - | |
| 386 | q->getContentsMargins(&leftMargin, &topMargin, &rightMargin, &bottomMargin); | - |
| 387 | int left, top, right, bottom; | - |
| 388 | effectiveMargins(&left, &top, &right, &bottom); | - |
| 389 | QSize extra(left + right, top + bottom); | - |
| 390 | - | |
| 391 | minSize += extra; | - |
| 392 | maxSize += extra; | - |
| 393 | sizeHint += extra; | - |
| 394 | - | |
| 395 | dirty = false; | - |
| 396 | } | - |
| 397 | - | |
| 398 | /* | - |
| 399 | Calculates and stores the preferred height given the width \a w. | - |
| 400 | */ | - |
| 401 | void QBoxLayoutPrivate::calcHfw(int w) | - |
| 402 | { | - |
| 403 | QVector<QLayoutStruct> &a = geomArray; | - |
| 404 | int n = a.count(); | - |
| 405 | int h = 0; | - |
| 406 | int mh = 0; | - |
| 407 | - | |
| 408 | Q_ASSERT(n == list.size()); | - |
| 409 | - | |
| 410 | if (horz(dir)) { | - |
| 411 | qGeomCalc(a, 0, n, 0, w); | - |
| 412 | for (int i = 0; i < n; i++) { | - |
| 413 | QBoxLayoutItem *box = list.at(i); | - |
| 414 | h = qMax(h, box->hfw(a.at(i).size)); | - |
| 415 | mh = qMax(mh, box->mhfw(a.at(i).size)); | - |
| 416 | } | - |
| 417 | } else { | - |
| 418 | for (int i = 0; i < n; ++i) { | - |
| 419 | QBoxLayoutItem *box = list.at(i); | - |
| 420 | int spacing = a.at(i).spacing; | - |
| 421 | h += box->hfw(w); | - |
| 422 | mh += box->mhfw(w); | - |
| 423 | h += spacing; | - |
| 424 | mh += spacing; | - |
| 425 | } | - |
| 426 | } | - |
| 427 | hfwWidth = w; | - |
| 428 | hfwHeight = h; | - |
| 429 | hfwMinHeight = mh; | - |
| 430 | } | - |
| 431 | - | |
| 432 | QLayoutItem* QBoxLayoutPrivate::replaceAt(int index, QLayoutItem *item) | - |
| 433 | { | - |
| 434 | Q_Q(QBoxLayout); | - |
| 435 | if (!item) | - |
| 436 | return 0; | - |
| 437 | QBoxLayoutItem *b = list.value(index); | - |
| 438 | if (!b) | - |
| 439 | return 0; | - |
| 440 | QLayoutItem *r = b->item; | - |
| 441 | - | |
| 442 | b->item = item; | - |
| 443 | q->invalidate(); | - |
| 444 | return r; | - |
| 445 | } | - |
| 446 | - | |
| 447 | - | |
| 448 | /*! | - |
| 449 | \class QBoxLayout | - |
| 450 | - | |
| 451 | \brief The QBoxLayout class lines up child widgets horizontally or | - |
| 452 | vertically. | - |
| 453 | - | |
| 454 | \ingroup geomanagement | - |
| 455 | \inmodule QtWidgets | - |
| 456 | - | |
| 457 | QBoxLayout takes the space it gets (from its parent layout or from | - |
| 458 | the parentWidget()), divides it up into a row of boxes, and makes | - |
| 459 | each managed widget fill one box. | - |
| 460 | - | |
| 461 | \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets | - |
| 462 | - | |
| 463 | If the QBoxLayout's orientation is Qt::Horizontal the boxes are | - |
| 464 | placed in a row, with suitable sizes. Each widget (or other box) | - |
| 465 | will get at least its minimum size and at most its maximum size. | - |
| 466 | Any excess space is shared according to the stretch factors (more | - |
| 467 | about that below). | - |
| 468 | - | |
| 469 | \image qvboxlayout-with-5-children.png Vertical box layout with five child widgets | - |
| 470 | - | |
| 471 | If the QBoxLayout's orientation is Qt::Vertical, the boxes are | - |
| 472 | placed in a column, again with suitable sizes. | - |
| 473 | - | |
| 474 | The easiest way to create a QBoxLayout is to use one of the | - |
| 475 | convenience classes, e.g. QHBoxLayout (for Qt::Horizontal boxes) | - |
| 476 | or QVBoxLayout (for Qt::Vertical boxes). You can also use the | - |
| 477 | QBoxLayout constructor directly, specifying its direction as | - |
| 478 | LeftToRight, RightToLeft, TopToBottom, or BottomToTop. | - |
| 479 | - | |
| 480 | If the QBoxLayout is not the top-level layout (i.e. it is not | - |
| 481 | managing all of the widget's area and children), you must add it | - |
| 482 | to its parent layout before you can do anything with it. The | - |
| 483 | normal way to add a layout is by calling | - |
| 484 | parentLayout-\>addLayout(). | - |
| 485 | - | |
| 486 | Once you have done this, you can add boxes to the QBoxLayout using | - |
| 487 | one of four functions: | - |
| 488 | - | |
| 489 | \list | - |
| 490 | \li addWidget() to add a widget to the QBoxLayout and set the | - |
| 491 | widget's stretch factor. (The stretch factor is along the row of | - |
| 492 | boxes.) | - |
| 493 | - | |
| 494 | \li addSpacing() to create an empty box; this is one of the | - |
| 495 | functions you use to create nice and spacious dialogs. See below | - |
| 496 | for ways to set margins. | - |
| 497 | - | |
| 498 | \li addStretch() to create an empty, stretchable box. | - |
| 499 | - | |
| 500 | \li addLayout() to add a box containing another QLayout to the row | - |
| 501 | and set that layout's stretch factor. | - |
| 502 | \endlist | - |
| 503 | - | |
| 504 | Use insertWidget(), insertSpacing(), insertStretch() or | - |
| 505 | insertLayout() to insert a box at a specified position in the | - |
| 506 | layout. | - |
| 507 | - | |
| 508 | QBoxLayout also includes two margin widths: | - |
| 509 | - | |
| 510 | \list | - |
| 511 | \li setContentsMargins() sets the width of the outer border on | - |
| 512 | each side of the widget. This is the width of the reserved space | - |
| 513 | along each of the QBoxLayout's four sides. | - |
| 514 | \li setSpacing() sets the width between neighboring boxes. (You | - |
| 515 | can use addSpacing() to get more space at a particular spot.) | - |
| 516 | \endlist | - |
| 517 | - | |
| 518 | The margin default is provided by the style. The default margin | - |
| 519 | most Qt styles specify is 9 for child widgets and 11 for windows. | - |
| 520 | The spacing defaults to the same as the margin width for a | - |
| 521 | top-level layout, or to the same as the parent layout. | - |
| 522 | - | |
| 523 | To remove a widget from a layout, call removeWidget(). Calling | - |
| 524 | QWidget::hide() on a widget also effectively removes the widget | - |
| 525 | from the layout until QWidget::show() is called. | - |
| 526 | - | |
| 527 | You will almost always want to use QVBoxLayout and QHBoxLayout | - |
| 528 | rather than QBoxLayout because of their convenient constructors. | - |
| 529 | - | |
| 530 | \sa QGridLayout, QStackedLayout, {Layout Management} | - |
| 531 | */ | - |
| 532 | - | |
| 533 | /*! | - |
| 534 | \enum QBoxLayout::Direction | - |
| 535 | - | |
| 536 | This type is used to determine the direction of a box layout. | - |
| 537 | - | |
| 538 | \value LeftToRight Horizontal from left to right. | - |
| 539 | \value RightToLeft Horizontal from right to left. | - |
| 540 | \value TopToBottom Vertical from top to bottom. | - |
| 541 | \value BottomToTop Vertical from bottom to top. | - |
| 542 | - | |
| 543 | \omitvalue Down | - |
| 544 | \omitvalue Up | - |
| 545 | */ | - |
| 546 | - | |
| 547 | /*! | - |
| 548 | Constructs a new QBoxLayout with direction \a dir and parent widget \a | - |
| 549 | parent. | - |
| 550 | - | |
| 551 | \sa direction() | - |
| 552 | */ | - |
| 553 | QBoxLayout::QBoxLayout(Direction dir, QWidget *parent) | - |
| 554 | : QLayout(*new QBoxLayoutPrivate, 0, parent) | - |
| 555 | { | - |
| 556 | Q_D(QBoxLayout); | - |
| 557 | d->dir = dir; | - |
| 558 | } | - |
| 559 | - | |
| 560 | - | |
| 561 | - | |
| 562 | /*! | - |
| 563 | Destroys this box layout. | - |
| 564 | - | |
| 565 | The layout's widgets aren't destroyed. | - |
| 566 | */ | - |
| 567 | QBoxLayout::~QBoxLayout() | - |
| 568 | { | - |
| 569 | Q_D(QBoxLayout); | - |
| 570 | d->deleteAll(); // must do it before QObject deletes children, so can't be in ~QBoxLayoutPrivate | - |
| 571 | } | - |
| 572 | - | |
| 573 | /*! | - |
| 574 | Reimplements QLayout::spacing(). If the spacing property is | - |
| 575 | valid, that value is returned. Otherwise, a value for the spacing | - |
| 576 | property is computed and returned. Since layout spacing in a widget | - |
| 577 | is style dependent, if the parent is a widget, it queries the style | - |
| 578 | for the (horizontal or vertical) spacing of the layout. Otherwise, | - |
| 579 | the parent is a layout, and it queries the parent layout for the | - |
| 580 | spacing(). | - |
| 581 | - | |
| 582 | \sa QLayout::spacing(), setSpacing() | - |
| 583 | */ | - |
| 584 | int QBoxLayout::spacing() const | - |
| 585 | { | - |
| 586 | Q_D(const QBoxLayout); | - |
| 587 | if (d->spacing >=0) { | - |
| 588 | return d->spacing; | - |
| 589 | } else { | - |
| 590 | return qSmartSpacing(this, d->dir == LeftToRight || d->dir == RightToLeft | - |
| 591 | ? QStyle::PM_LayoutHorizontalSpacing | - |
| 592 | : QStyle::PM_LayoutVerticalSpacing); | - |
| 593 | } | - |
| 594 | } | - |
| 595 | - | |
| 596 | /*! | - |
| 597 | Reimplements QLayout::setSpacing(). Sets the spacing | - |
| 598 | property to \a spacing. | - |
| 599 | - | |
| 600 | \sa QLayout::setSpacing(), spacing() | - |
| 601 | */ | - |
| 602 | void QBoxLayout::setSpacing(int spacing) | - |
| 603 | { | - |
| 604 | Q_D(QBoxLayout); | - |
| 605 | d->spacing = spacing; | - |
| 606 | invalidate(); | - |
| 607 | } | - |
| 608 | - | |
| 609 | /*! | - |
| 610 | \reimp | - |
| 611 | */ | - |
| 612 | QSize QBoxLayout::sizeHint() const | - |
| 613 | { | - |
| 614 | Q_D(const QBoxLayout); | - |
| 615 | if (d->dirty) | - |
| 616 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); | - |
| 617 | return d->sizeHint; | - |
| 618 | } | - |
| 619 | - | |
| 620 | /*! | - |
| 621 | \reimp | - |
| 622 | */ | - |
| 623 | QSize QBoxLayout::minimumSize() const | - |
| 624 | { | - |
| 625 | Q_D(const QBoxLayout); | - |
| 626 | if (d->dirty) | - |
| 627 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); | - |
| 628 | return d->minSize; | - |
| 629 | } | - |
| 630 | - | |
| 631 | /*! | - |
| 632 | \reimp | - |
| 633 | */ | - |
| 634 | QSize QBoxLayout::maximumSize() const | - |
| 635 | { | - |
| 636 | Q_D(const QBoxLayout); | - |
| 637 | if (d->dirty) | - |
| 638 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); | - |
| 639 | - | |
| 640 | QSize s = d->maxSize.boundedTo(QSize(QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX)); | - |
| 641 | - | |
| 642 | if (alignment() & Qt::AlignHorizontal_Mask) | - |
| 643 | s.setWidth(QLAYOUTSIZE_MAX); | - |
| 644 | if (alignment() & Qt::AlignVertical_Mask) | - |
| 645 | s.setHeight(QLAYOUTSIZE_MAX); | - |
| 646 | return s; | - |
| 647 | } | - |
| 648 | - | |
| 649 | /*! | - |
| 650 | \reimp | - |
| 651 | */ | - |
| 652 | bool QBoxLayout::hasHeightForWidth() const | - |
| 653 | { | - |
| 654 | Q_D(const QBoxLayout); | - |
| 655 | if (d->dirty) | - |
| 656 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); | - |
| 657 | return d->hasHfw; | - |
| 658 | } | - |
| 659 | - | |
| 660 | /*! | - |
| 661 | \reimp | - |
| 662 | */ | - |
| 663 | int QBoxLayout::heightForWidth(int w) const | - |
| 664 | { | - |
| 665 | Q_D(const QBoxLayout); | - |
| 666 | if (!hasHeightForWidth()) | - |
| 667 | return -1; | - |
| 668 | - | |
| 669 | int left, top, right, bottom; | - |
| 670 | d->effectiveMargins(&left, &top, &right, &bottom); | - |
| 671 | - | |
| 672 | w -= left + right; | - |
| 673 | if (w != d->hfwWidth) | - |
| 674 | const_cast<QBoxLayout*>(this)->d_func()->calcHfw(w); | - |
| 675 | - | |
| 676 | return d->hfwHeight + top + bottom; | - |
| 677 | } | - |
| 678 | - | |
| 679 | /*! | - |
| 680 | \reimp | - |
| 681 | */ | - |
| 682 | int QBoxLayout::minimumHeightForWidth(int w) const | - |
| 683 | { | - |
| 684 | Q_D(const QBoxLayout); | - |
| 685 | (void) heightForWidth(w); | - |
| 686 | int top, bottom; | - |
| 687 | d->effectiveMargins(0, &top, 0, &bottom); | - |
| 688 | return d->hasHfw ? (d->hfwMinHeight + top + bottom) : -1; | - |
| 689 | } | - |
| 690 | - | |
| 691 | /*! | - |
| 692 | Resets cached information. | - |
| 693 | */ | - |
| 694 | void QBoxLayout::invalidate() | - |
| 695 | { | - |
| 696 | Q_D(QBoxLayout); | - |
| 697 | d->setDirty(); | - |
| 698 | QLayout::invalidate(); | - |
| 699 | } | - |
| 700 | - | |
| 701 | /*! | - |
| 702 | \reimp | - |
| 703 | */ | - |
| 704 | int QBoxLayout::count() const | - |
| 705 | { | - |
| 706 | Q_D(const QBoxLayout); | - |
| 707 | return d->list.count(); | - |
| 708 | } | - |
| 709 | - | |
| 710 | /*! | - |
| 711 | \reimp | - |
| 712 | */ | - |
| 713 | QLayoutItem *QBoxLayout::itemAt(int index) const | - |
| 714 | { | - |
| 715 | Q_D(const QBoxLayout); | - |
| 716 | return index >= 0 && index < d->list.count() ? d->list.at(index)->item : 0; | - |
| 717 | } | - |
| 718 | - | |
| 719 | /*! | - |
| 720 | \reimp | - |
| 721 | */ | - |
| 722 | QLayoutItem *QBoxLayout::takeAt(int index) | - |
| 723 | { | - |
| 724 | Q_D(QBoxLayout); | - |
| 725 | if (index < 0 || index >= d->list.count()) | - |
| 726 | return 0; | - |
| 727 | QBoxLayoutItem *b = d->list.takeAt(index); | - |
| 728 | QLayoutItem *item = b->item; | - |
| 729 | b->item = 0; | - |
| 730 | delete b; | - |
| 731 | - | |
| 732 | if (QLayout *l = item->layout()) { | - |
| 733 | // sanity check in case the user passed something weird to QObject::setParent() | - |
| 734 | if (l->parent() == this) | - |
| 735 | l->setParent(0); | - |
| 736 | } | - |
| 737 | - | |
| 738 | invalidate(); | - |
| 739 | return item; | - |
| 740 | } | - |
| 741 | - | |
| 742 | - | |
| 743 | /*! | - |
| 744 | \reimp | - |
| 745 | */ | - |
| 746 | Qt::Orientations QBoxLayout::expandingDirections() const | - |
| 747 | { | - |
| 748 | Q_D(const QBoxLayout); | - |
| 749 | if (d->dirty) | - |
| 750 | const_cast<QBoxLayout*>(this)->d_func()->setupGeom(); | - |
| 751 | return d->expanding; | - |
| 752 | } | - |
| 753 | - | |
| 754 | /*! | - |
| 755 | \reimp | - |
| 756 | */ | - |
| 757 | void QBoxLayout::setGeometry(const QRect &r) | - |
| 758 | { | - |
| 759 | Q_D(QBoxLayout); | - |
| 760 | if (d->dirty || r != geometry()) { | - |
| 761 | QRect oldRect = geometry(); | - |
| 762 | QLayout::setGeometry(r); | - |
| 763 | if (d->dirty) | - |
| 764 | d->setupGeom(); | - |
| 765 | QRect cr = alignment() ? alignmentRect(r) : r; | - |
| 766 | - | |
| 767 | int left, top, right, bottom; | - |
| 768 | d->effectiveMargins(&left, &top, &right, &bottom); | - |
| 769 | QRect s(cr.x() + left, cr.y() + top, | - |
| 770 | cr.width() - (left + right), | - |
| 771 | cr.height() - (top + bottom)); | - |
| 772 | - | |
| 773 | QVector<QLayoutStruct> a = d->geomArray; | - |
| 774 | int pos = horz(d->dir) ? s.x() : s.y(); | - |
| 775 | int space = horz(d->dir) ? s.width() : s.height(); | - |
| 776 | int n = a.count(); | - |
| 777 | if (d->hasHfw && !horz(d->dir)) { | - |
| 778 | for (int i = 0; i < n; i++) { | - |
| 779 | QBoxLayoutItem *box = d->list.at(i); | - |
| 780 | if (box->item->hasHeightForWidth()) { | - |
| 781 | int width = qBound(box->item->minimumSize().width(), s.width(), box->item->maximumSize().width()); | - |
| 782 | a[i].sizeHint = a[i].minimumSize = | - |
| 783 | box->item->heightForWidth(width); | - |
| 784 | } | - |
| 785 | } | - |
| 786 | } | - |
| 787 | - | |
| 788 | Direction visualDir = d->dir; | - |
| 789 | QWidget *parent = parentWidget(); | - |
| 790 | if (parent && parent->isRightToLeft()) { | - |
| 791 | if (d->dir == LeftToRight) | - |
| 792 | visualDir = RightToLeft; | - |
| 793 | else if (d->dir == RightToLeft) | - |
| 794 | visualDir = LeftToRight; | - |
| 795 | } | - |
| 796 | - | |
| 797 | qGeomCalc(a, 0, n, pos, space); | - |
| 798 | - | |
| 799 | bool reverse = (horz(visualDir) | - |
| 800 | ? ((r.right() > oldRect.right()) != (visualDir == RightToLeft)) | - |
| 801 | : r.bottom() > oldRect.bottom()); | - |
| 802 | for (int j = 0; j < n; j++) { | - |
| 803 | int i = reverse ? n-j-1 : j; | - |
| 804 | QBoxLayoutItem *box = d->list.at(i); | - |
| 805 | - | |
| 806 | switch (visualDir) { | - |
| 807 | case LeftToRight: | - |
| 808 | box->item->setGeometry(QRect(a.at(i).pos, s.y(), a.at(i).size, s.height())); | - |
| 809 | break; | - |
| 810 | case RightToLeft: | - |
| 811 | box->item->setGeometry(QRect(s.left() + s.right() - a.at(i).pos - a.at(i).size + 1, | - |
| 812 | s.y(), a.at(i).size, s.height())); | - |
| 813 | break; | - |
| 814 | case TopToBottom: | - |
| 815 | box->item->setGeometry(QRect(s.x(), a.at(i).pos, s.width(), a.at(i).size)); | - |
| 816 | break; | - |
| 817 | case BottomToTop: | - |
| 818 | box->item->setGeometry(QRect(s.x(), | - |
| 819 | s.top() + s.bottom() - a.at(i).pos - a.at(i).size + 1, | - |
| 820 | s.width(), a.at(i).size)); | - |
| 821 | } | - |
| 822 | } | - |
| 823 | } | - |
| 824 | } | - |
| 825 | - | |
| 826 | /*! | - |
| 827 | \reimp | - |
| 828 | */ | - |
| 829 | void QBoxLayout::addItem(QLayoutItem *item) | - |
| 830 | { | - |
| 831 | Q_D(QBoxLayout); | - |
| 832 | QBoxLayoutItem *it = new QBoxLayoutItem(item); | - |
| 833 | d->list.append(it); | - |
| 834 | invalidate(); | - |
| 835 | } | - |
| 836 | - | |
| 837 | /*! | - |
| 838 | Inserts \a item into this box layout at position \a index. If \a | - |
| 839 | index is negative, the item is added at the end. | - |
| 840 | - | |
| 841 | \sa addItem(), insertWidget(), insertLayout(), insertStretch(), | - |
| 842 | insertSpacing() | - |
| 843 | */ | - |
| 844 | void QBoxLayout::insertItem(int index, QLayoutItem *item) | - |
| 845 | { | - |
| 846 | Q_D(QBoxLayout); | - |
| 847 | if (index < 0) // append | - |
| 848 | index = d->list.count(); | - |
| 849 | - | |
| 850 | QBoxLayoutItem *it = new QBoxLayoutItem(item); | - |
| 851 | d->list.insert(index, it); | - |
| 852 | invalidate(); | - |
| 853 | } | - |
| 854 | - | |
| 855 | /*! | - |
| 856 | Inserts a non-stretchable space (a QSpacerItem) at position \a index, with | - |
| 857 | size \a size. If \a index is negative the space is added at the end. | - |
| 858 | - | |
| 859 | The box layout has default margin and spacing. This function adds | - |
| 860 | additional space. | - |
| 861 | - | |
| 862 | \sa addSpacing(), insertItem(), QSpacerItem | - |
| 863 | */ | - |
| 864 | void QBoxLayout::insertSpacing(int index, int size) | - |
| 865 | { | - |
| 866 | Q_D(QBoxLayout); | - |
| 867 | if (index < 0) // append | - |
| 868 | index = d->list.count(); | - |
| 869 | - | |
| 870 | QLayoutItem *b; | - |
| 871 | if (horz(d->dir)) | - |
| 872 | b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Fixed, QSizePolicy::Minimum); | - |
| 873 | else | - |
| 874 | b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Minimum, QSizePolicy::Fixed); | - |
| 875 | - | |
| 876 | QT_TRY { | - |
| 877 | QBoxLayoutItem *it = new QBoxLayoutItem(b); | - |
| 878 | it->magic = true; | - |
| 879 | d->list.insert(index, it); | - |
| 880 | - | |
| 881 | } QT_CATCH(...) { dead code: { delete b; qt_noop(); } | - |
| 882 | delete b; dead code: { delete b; qt_noop(); } | - |
| 883 | QT_RETHROW; dead code: { delete b; qt_noop(); } | - |
| 884 | } dead code: { delete b; qt_noop(); } | - |
| 885 | invalidate(); | - |
| 886 | } | - |
| 887 | - | |
| 888 | /*! | - |
| 889 | Inserts a stretchable space (a QSpacerItem) at position \a | - |
| 890 | index, with zero minimum size and stretch factor \a stretch. If \a | - |
| 891 | index is negative the space is added at the end. | - |
| 892 | - | |
| 893 | \sa addStretch(), insertItem(), QSpacerItem | - |
| 894 | */ | - |
| 895 | void QBoxLayout::insertStretch(int index, int stretch) | - |
| 896 | { | - |
| 897 | Q_D(QBoxLayout); | - |
| 898 | if (index < 0) // append | - |
| 899 | index = d->list.count(); | - |
| 900 | - | |
| 901 | QLayoutItem *b; | - |
| 902 | if (horz(d->dir)) | - |
| 903 | b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum); | - |
| 904 | else | - |
| 905 | b = QLayoutPrivate::createSpacerItem(this, 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding); | - |
| 906 | - | |
| 907 | QBoxLayoutItem *it = new QBoxLayoutItem(b, stretch); | - |
| 908 | it->magic = true; | - |
| 909 | d->list.insert(index, it); | - |
| 910 | invalidate(); | - |
| 911 | } | - |
| 912 | - | |
| 913 | /*! | - |
| 914 | \since 4.4 | - |
| 915 | - | |
| 916 | Inserts \a spacerItem at position \a index, with zero minimum | - |
| 917 | size and stretch factor. If \a index is negative the | - |
| 918 | space is added at the end. | - |
| 919 | - | |
| 920 | \sa addSpacerItem(), insertStretch(), insertSpacing() | - |
| 921 | */ | - |
| 922 | void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem) | - |
| 923 | { | - |
| 924 | Q_D(QBoxLayout); | - |
| 925 | if (index < 0) // append | - |
| 926 | index = d->list.count(); | - |
| 927 | - | |
| 928 | QBoxLayoutItem *it = new QBoxLayoutItem(spacerItem); | - |
| 929 | it->magic = true; | - |
| 930 | d->list.insert(index, it); | - |
| 931 | invalidate(); | - |
| 932 | } | - |
| 933 | - | |
| 934 | /*! | - |
| 935 | Inserts \a layout at position \a index, with stretch factor \a | - |
| 936 | stretch. If \a index is negative, the layout is added at the end. | - |
| 937 | - | |
| 938 | \a layout becomes a child of the box layout. | - |
| 939 | - | |
| 940 | \sa addLayout(), insertItem() | - |
| 941 | */ | - |
| 942 | void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch) | - |
| 943 | { | - |
| 944 | Q_D(QBoxLayout); | - |
| 945 | if (!d->checkLayout(layout)) | - |
| 946 | return; | - |
| 947 | if (!adoptLayout(layout)) | - |
| 948 | return; | - |
| 949 | if (index < 0) // append | - |
| 950 | index = d->list.count(); | - |
| 951 | QBoxLayoutItem *it = new QBoxLayoutItem(layout, stretch); | - |
| 952 | d->list.insert(index, it); | - |
| 953 | invalidate(); | - |
| 954 | } | - |
| 955 | - | |
| 956 | /*! | - |
| 957 | Inserts \a widget at position \a index, with stretch factor \a | - |
| 958 | stretch and alignment \a alignment. If \a index is negative, the | - |
| 959 | widget is added at the end. | - |
| 960 | - | |
| 961 | The stretch factor applies only in the \l{direction()}{direction} | - |
| 962 | of the QBoxLayout, and is relative to the other boxes and widgets | - |
| 963 | in this QBoxLayout. Widgets and boxes with higher stretch factors | - |
| 964 | grow more. | - |
| 965 | - | |
| 966 | If the stretch factor is 0 and nothing else in the QBoxLayout has | - |
| 967 | a stretch factor greater than zero, the space is distributed | - |
| 968 | according to the QWidget:sizePolicy() of each widget that's | - |
| 969 | involved. | - |
| 970 | - | |
| 971 | The alignment is specified by \a alignment. The default alignment | - |
| 972 | is 0, which means that the widget fills the entire cell. | - |
| 973 | - | |
| 974 | \sa addWidget(), insertItem() | - |
| 975 | */ | - |
| 976 | void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch, | - |
| 977 | Qt::Alignment alignment) | - |
| 978 | { | - |
| 979 | Q_D(QBoxLayout); | - |
| 980 | if (!d->checkWidget(widget)) | - |
| 981 | return; | - |
| 982 | addChildWidget(widget); | - |
| 983 | if (index < 0) // append | - |
| 984 | index = d->list.count(); | - |
| 985 | QWidgetItem *b = QLayoutPrivate::createWidgetItem(this, widget); | - |
| 986 | b->setAlignment(alignment); | - |
| 987 | - | |
| 988 | QBoxLayoutItem *it; | - |
| 989 | QT_TRY{ | - |
| 990 | it = new QBoxLayoutItem(b, stretch); | - |
| 991 | } QT_CATCH(...) { dead code: { delete b; qt_noop(); } | - |
| 992 | delete b; dead code: { delete b; qt_noop(); } | - |
| 993 | QT_RETHROW; dead code: { delete b; qt_noop(); } | - |
| 994 | } dead code: { delete b; qt_noop(); } | - |
| 995 | - | |
| 996 | QT_TRY{ | - |
| 997 | d->list.insert(index, it); | - |
| 998 | } QT_CATCH(...) { dead code: { delete it; qt_noop(); } | - |
| 999 | delete it; dead code: { delete it; qt_noop(); } | - |
| 1000 | QT_RETHROW; dead code: { delete it; qt_noop(); } | - |
| 1001 | } dead code: { delete it; qt_noop(); } | - |
| 1002 | invalidate(); | - |
| 1003 | } | - |
| 1004 | - | |
| 1005 | /*! | - |
| 1006 | Adds a non-stretchable space (a QSpacerItem) with size \a size | - |
| 1007 | to the end of this box layout. QBoxLayout provides default margin | - |
| 1008 | and spacing. This function adds additional space. | - |
| 1009 | - | |
| 1010 | \sa insertSpacing(), addItem(), QSpacerItem | - |
| 1011 | */ | - |
| 1012 | void QBoxLayout::addSpacing(int size) | - |
| 1013 | { | - |
| 1014 | insertSpacing(-1, size); | - |
| 1015 | } | - |
| 1016 | - | |
| 1017 | /*! | - |
| 1018 | Adds a stretchable space (a QSpacerItem) with zero minimum | - |
| 1019 | size and stretch factor \a stretch to the end of this box layout. | - |
| 1020 | - | |
| 1021 | \sa insertStretch(), addItem(), QSpacerItem | - |
| 1022 | */ | - |
| 1023 | void QBoxLayout::addStretch(int stretch) | - |
| 1024 | { | - |
| 1025 | insertStretch(-1, stretch); | - |
| 1026 | } | - |
| 1027 | - | |
| 1028 | /*! | - |
| 1029 | \since 4.4 | - |
| 1030 | - | |
| 1031 | Adds \a spacerItem to the end of this box layout. | - |
| 1032 | - | |
| 1033 | \sa addSpacing(), addStretch() | - |
| 1034 | */ | - |
| 1035 | void QBoxLayout::addSpacerItem(QSpacerItem *spacerItem) | - |
| 1036 | { | - |
| 1037 | insertSpacerItem(-1, spacerItem); | - |
| 1038 | } | - |
| 1039 | - | |
| 1040 | /*! | - |
| 1041 | Adds \a widget to the end of this box layout, with a stretch | - |
| 1042 | factor of \a stretch and alignment \a alignment. | - |
| 1043 | - | |
| 1044 | The stretch factor applies only in the \l{direction()}{direction} | - |
| 1045 | of the QBoxLayout, and is relative to the other boxes and widgets | - |
| 1046 | in this QBoxLayout. Widgets and boxes with higher stretch factors | - |
| 1047 | grow more. | - |
| 1048 | - | |
| 1049 | If the stretch factor is 0 and nothing else in the QBoxLayout has | - |
| 1050 | a stretch factor greater than zero, the space is distributed | - |
| 1051 | according to the QWidget:sizePolicy() of each widget that's | - |
| 1052 | involved. | - |
| 1053 | - | |
| 1054 | The alignment is specified by \a alignment. The default | - |
| 1055 | alignment is 0, which means that the widget fills the entire cell. | - |
| 1056 | - | |
| 1057 | \sa insertWidget(), addItem(), addLayout(), addStretch(), | - |
| 1058 | addSpacing(), addStrut() | - |
| 1059 | */ | - |
| 1060 | void QBoxLayout::addWidget(QWidget *widget, int stretch, Qt::Alignment alignment) | - |
| 1061 | { | - |
| 1062 | insertWidget(-1, widget, stretch, alignment); | - |
| 1063 | } | - |
| 1064 | - | |
| 1065 | /*! | - |
| 1066 | Adds \a layout to the end of the box, with serial stretch factor | - |
| 1067 | \a stretch. | - |
| 1068 | - | |
| 1069 | \sa insertLayout(), addItem(), addWidget() | - |
| 1070 | */ | - |
| 1071 | void QBoxLayout::addLayout(QLayout *layout, int stretch) | - |
| 1072 | { | - |
| 1073 | insertLayout(-1, layout, stretch); | - |
| 1074 | } | - |
| 1075 | - | |
| 1076 | /*! | - |
| 1077 | Limits the perpendicular dimension of the box (e.g. height if the | - |
| 1078 | box is \l LeftToRight) to a minimum of \a size. Other constraints | - |
| 1079 | may increase the limit. | - |
| 1080 | - | |
| 1081 | \sa addItem() | - |
| 1082 | */ | - |
| 1083 | void QBoxLayout::addStrut(int size) | - |
| 1084 | { | - |
| 1085 | Q_D(QBoxLayout); | - |
| 1086 | QLayoutItem *b; | - |
| 1087 | if (horz(d->dir)) | - |
| 1088 | b = QLayoutPrivate::createSpacerItem(this, 0, size, QSizePolicy::Fixed, QSizePolicy::Minimum); | - |
| 1089 | else | - |
| 1090 | b = QLayoutPrivate::createSpacerItem(this, size, 0, QSizePolicy::Minimum, QSizePolicy::Fixed); | - |
| 1091 | - | |
| 1092 | QBoxLayoutItem *it = new QBoxLayoutItem(b); | - |
| 1093 | it->magic = true; | - |
| 1094 | d->list.append(it); | - |
| 1095 | invalidate(); | - |
| 1096 | } | - |
| 1097 | - | |
| 1098 | /*! | - |
| 1099 | Sets the stretch factor for \a widget to \a stretch and returns | - |
| 1100 | true if \a widget is found in this layout (not including child | - |
| 1101 | layouts); otherwise returns \c false. | - |
| 1102 | - | |
| 1103 | \sa setAlignment() | - |
| 1104 | */ | - |
| 1105 | bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch) | - |
| 1106 | { | - |
| 1107 | Q_D(QBoxLayout); | - |
| 1108 | if (!widget) | - |
| 1109 | return false; | - |
| 1110 | for (int i = 0; i < d->list.size(); ++i) { | - |
| 1111 | QBoxLayoutItem *box = d->list.at(i); | - |
| 1112 | if (box->item->widget() == widget) { | - |
| 1113 | box->stretch = stretch; | - |
| 1114 | invalidate(); | - |
| 1115 | return true; | - |
| 1116 | } | - |
| 1117 | } | - |
| 1118 | return false; | - |
| 1119 | } | - |
| 1120 | - | |
| 1121 | /*! | - |
| 1122 | \overload | - |
| 1123 | - | |
| 1124 | Sets the stretch factor for the layout \a layout to \a stretch and | - |
| 1125 | returns \c true if \a layout is found in this layout (not including | - |
| 1126 | child layouts); otherwise returns \c false. | - |
| 1127 | */ | - |
| 1128 | bool QBoxLayout::setStretchFactor(QLayout *layout, int stretch) | - |
| 1129 | { | - |
| 1130 | Q_D(QBoxLayout); | - |
| 1131 | for (int i = 0; i < d->list.size(); ++i) { | - |
| 1132 | QBoxLayoutItem *box = d->list.at(i); | - |
| 1133 | if (box->item->layout() == layout) { | - |
| 1134 | if (box->stretch != stretch) { | - |
| 1135 | box->stretch = stretch; | - |
| 1136 | invalidate(); | - |
| 1137 | } | - |
| 1138 | return true; | - |
| 1139 | } | - |
| 1140 | } | - |
| 1141 | return false; | - |
| 1142 | } | - |
| 1143 | - | |
| 1144 | /*! | - |
| 1145 | Sets the stretch factor at position \a index. to \a stretch. | - |
| 1146 | - | |
| 1147 | \since 4.5 | - |
| 1148 | */ | - |
| 1149 | - | |
| 1150 | void QBoxLayout::setStretch(int index, int stretch) | - |
| 1151 | { | - |
| 1152 | Q_D(QBoxLayout); | - |
| 1153 | if (index >= 0 && index < d->list.size()) { | - |
| 1154 | QBoxLayoutItem *box = d->list.at(index); | - |
| 1155 | if (box->stretch != stretch) { | - |
| 1156 | box->stretch = stretch; | - |
| 1157 | invalidate(); | - |
| 1158 | } | - |
| 1159 | } | - |
| 1160 | } | - |
| 1161 | - | |
| 1162 | /*! | - |
| 1163 | Returns the stretch factor at position \a index. | - |
| 1164 | - | |
| 1165 | \since 4.5 | - |
| 1166 | */ | - |
| 1167 | - | |
| 1168 | int QBoxLayout::stretch(int index) const | - |
| 1169 | { | - |
| 1170 | Q_D(const QBoxLayout); | - |
| 1171 | if (index >= 0 && index < d->list.size()) | - |
| 1172 | return d->list.at(index)->stretch; | - |
| 1173 | return -1; | - |
| 1174 | } | - |
| 1175 | - | |
| 1176 | /*! | - |
| 1177 | Sets the direction of this layout to \a direction. | - |
| 1178 | */ | - |
| 1179 | void QBoxLayout::setDirection(Direction direction) | - |
| 1180 | { | - |
| 1181 | Q_D(QBoxLayout); | - |
| 1182 | if (d->dir == direction) | - |
| 1183 | return; | - |
| 1184 | if (horz(d->dir) != horz(direction)) { | - |
| 1185 | //swap around the spacers (the "magic" bits) | - |
| 1186 | //#### a bit yucky, knows too much. | - |
| 1187 | //#### probably best to add access functions to spacerItem | - |
| 1188 | //#### or even a QSpacerItem::flip() | - |
| 1189 | for (int i = 0; i < d->list.size(); ++i) { | - |
| 1190 | QBoxLayoutItem *box = d->list.at(i); | - |
| 1191 | if (box->magic) { | - |
| 1192 | QSpacerItem *sp = box->item->spacerItem(); | - |
| 1193 | if (sp) { | - |
| 1194 | if (sp->expandingDirections() == Qt::Orientations(0) /*No Direction*/) { | - |
| 1195 | //spacing or strut | - |
| 1196 | QSize s = sp->sizeHint(); | - |
| 1197 | sp->changeSize(s.height(), s.width(), | - |
| 1198 | horz(direction) ? QSizePolicy::Fixed:QSizePolicy::Minimum, | - |
| 1199 | horz(direction) ? QSizePolicy::Minimum:QSizePolicy::Fixed); | - |
| 1200 | - | |
| 1201 | } else { | - |
| 1202 | //stretch | - |
| 1203 | if (horz(direction)) | - |
| 1204 | sp->changeSize(0, 0, QSizePolicy::Expanding, | - |
| 1205 | QSizePolicy::Minimum); | - |
| 1206 | else | - |
| 1207 | sp->changeSize(0, 0, QSizePolicy::Minimum, | - |
| 1208 | QSizePolicy::Expanding); | - |
| 1209 | } | - |
| 1210 | } | - |
| 1211 | } | - |
| 1212 | } | - |
| 1213 | } | - |
| 1214 | d->dir = direction; | - |
| 1215 | invalidate(); | - |
| 1216 | } | - |
| 1217 | - | |
| 1218 | /*! | - |
| 1219 | \fn QBoxLayout::Direction QBoxLayout::direction() const | - |
| 1220 | - | |
| 1221 | Returns the direction of the box. addWidget() and addSpacing() | - |
| 1222 | work in this direction; the stretch stretches in this direction. | - |
| 1223 | - | |
| 1224 | \sa QBoxLayout::Direction, addWidget(), addSpacing() | - |
| 1225 | */ | - |
| 1226 | - | |
| 1227 | QBoxLayout::Direction QBoxLayout::direction() const | - |
| 1228 | { | - |
| 1229 | Q_D(const QBoxLayout); | - |
| 1230 | return d->dir; | - |
| 1231 | } | - |
| 1232 | - | |
| 1233 | /*! | - |
| 1234 | \class QHBoxLayout | - |
| 1235 | \brief The QHBoxLayout class lines up widgets horizontally. | - |
| 1236 | - | |
| 1237 | \ingroup geomanagement | - |
| 1238 | \inmodule QtWidgets | - |
| 1239 | - | |
| 1240 | This class is used to construct horizontal box layout objects. See | - |
| 1241 | QBoxLayout for details. | - |
| 1242 | - | |
| 1243 | The simplest use of the class is like this: | - |
| 1244 | - | |
| 1245 | \snippet layouts/layouts.cpp 0 | - |
| 1246 | \snippet layouts/layouts.cpp 1 | - |
| 1247 | \snippet layouts/layouts.cpp 2 | - |
| 1248 | \codeline | - |
| 1249 | \snippet layouts/layouts.cpp 3 | - |
| 1250 | \snippet layouts/layouts.cpp 4 | - |
| 1251 | \snippet layouts/layouts.cpp 5 | - |
| 1252 | - | |
| 1253 | First, we create the widgets we want in the layout. Then, we | - |
| 1254 | create the QHBoxLayout object and add the widgets into the | - |
| 1255 | layout. Finally, we call QWidget::setLayout() to install the | - |
| 1256 | QHBoxLayout object onto the widget. At that point, the widgets in | - |
| 1257 | the layout are reparented to have \c window as their parent. | - |
| 1258 | - | |
| 1259 | \image qhboxlayout-with-5-children.png Horizontal box layout with five child widgets | - |
| 1260 | - | |
| 1261 | \sa QVBoxLayout, QGridLayout, QStackedLayout, {Layout Management}, {Basic Layouts Example} | - |
| 1262 | */ | - |
| 1263 | - | |
| 1264 | - | |
| 1265 | /*! | - |
| 1266 | Constructs a new top-level horizontal box with | - |
| 1267 | parent \a parent. | - |
| 1268 | */ | - |
| 1269 | QHBoxLayout::QHBoxLayout(QWidget *parent) | - |
| 1270 | : QBoxLayout(LeftToRight, parent) | - |
| 1271 | { | - |
| 1272 | } | - |
| 1273 | - | |
| 1274 | /*! | - |
| 1275 | Constructs a new horizontal box. You must add | - |
| 1276 | it to another layout. | - |
| 1277 | */ | - |
| 1278 | QHBoxLayout::QHBoxLayout() | - |
| 1279 | : QBoxLayout(LeftToRight) | - |
| 1280 | { | - |
| 1281 | } | - |
| 1282 | - | |
| 1283 | - | |
| 1284 | - | |
| 1285 | - | |
| 1286 | - | |
| 1287 | /*! | - |
| 1288 | Destroys this box layout. | - |
| 1289 | - | |
| 1290 | The layout's widgets aren't destroyed. | - |
| 1291 | */ | - |
| 1292 | QHBoxLayout::~QHBoxLayout() | - |
| 1293 | { | - |
| 1294 | } | - |
| 1295 | - | |
| 1296 | /*! | - |
| 1297 | \class QVBoxLayout | - |
| 1298 | \brief The QVBoxLayout class lines up widgets vertically. | - |
| 1299 | - | |
| 1300 | \ingroup geomanagement | - |
| 1301 | \inmodule QtWidgets | - |
| 1302 | - | |
| 1303 | This class is used to construct vertical box layout objects. See | - |
| 1304 | QBoxLayout for details. | - |
| 1305 | - | |
| 1306 | The simplest use of the class is like this: | - |
| 1307 | - | |
| 1308 | \snippet layouts/layouts.cpp 6 | - |
| 1309 | \snippet layouts/layouts.cpp 7 | - |
| 1310 | \snippet layouts/layouts.cpp 8 | - |
| 1311 | \codeline | - |
| 1312 | \snippet layouts/layouts.cpp 9 | - |
| 1313 | \snippet layouts/layouts.cpp 10 | - |
| 1314 | \snippet layouts/layouts.cpp 11 | - |
| 1315 | - | |
| 1316 | First, we create the widgets we want in the layout. Then, we | - |
| 1317 | create the QVBoxLayout object and add the widgets into the | - |
| 1318 | layout. Finally, we call QWidget::setLayout() to install the | - |
| 1319 | QVBoxLayout object onto the widget. At that point, the widgets in | - |
| 1320 | the layout are reparented to have \c window as their parent. | - |
| 1321 | - | |
| 1322 | \image qvboxlayout-with-5-children.png Horizontal box layout with five child widgets | - |
| 1323 | - | |
| 1324 | \sa QHBoxLayout, QGridLayout, QStackedLayout, {Layout Management}, {Basic Layouts Example} | - |
| 1325 | */ | - |
| 1326 | - | |
| 1327 | /*! | - |
| 1328 | Constructs a new top-level vertical box with | - |
| 1329 | parent \a parent. | - |
| 1330 | */ | - |
| 1331 | QVBoxLayout::QVBoxLayout(QWidget *parent) | - |
| 1332 | : QBoxLayout(TopToBottom, parent) | - |
| 1333 | { | - |
| 1334 | } | - |
| 1335 | - | |
| 1336 | /*! | - |
| 1337 | Constructs a new vertical box. You must add | - |
| 1338 | it to another layout. | - |
| 1339 | - | |
| 1340 | */ | - |
| 1341 | QVBoxLayout::QVBoxLayout() | - |
| 1342 | : QBoxLayout(TopToBottom) | - |
| 1343 | { | - |
| 1344 | } | - |
| 1345 | - | |
| 1346 | - | |
| 1347 | /*! | - |
| 1348 | Destroys this box layout. | - |
| 1349 | - | |
| 1350 | The layout's widgets aren't destroyed. | - |
| 1351 | */ | - |
| 1352 | QVBoxLayout::~QVBoxLayout() | - |
| 1353 | { | - |
| 1354 | } | - |
| 1355 | - | |
| 1356 | QT_END_NAMESPACE | - |
| 1357 | - | |
| 1358 | #include "moc_qboxlayout.cpp" | - |
| Source code | Switch to Preprocessed file |