📄 qlayout.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qlayout.h"#include "qapplication.h"#include "qlayoutengine_p.h"#include "qmenubar.h"#include "qtoolbar.h"#include "qevent.h"#include "qstyle.h"#include "qvariant.h"#include "qwidget_p.h"#include "qlayout_p.h"static int menuBarHeightForWidth(QWidget *menubar, int w){ if (menubar && !menubar->isHidden() && !menubar->isWindow()) return menubar->heightForWidth(qMax(w, menubar->minimumWidth())); return 0;}/*! \class QLayout \brief The QLayout class is the base class of geometry managers. \ingroup appearance \ingroup geomanagement This is an abstract base class inherited by the concrete classes QBoxLayout, QGridLayout, and QStackedLayout. For users of QLayout subclasses or of QMainWindow there is seldom any need to use the basic functions provided by QLayout, such as setSizeConstraint() or setMenuBar(). See \l{Layout Classes} for more information. To make your own layout manager, implement the functions addItem(), sizeHint(), setGeometry(), itemAt() and takeAt(). You should also implement minimumSize() to ensure your layout isn't resized to zero size if there is too little space. To support children whose heights depend on their widths, implement hasHeightForWidth() and heightForWidth(). See the \l{layouts/borderlayout}{Border Layout} and \l{layouts/flowlayout}{Flow Layout} examples for more information about implementing custom layout managers. Geometry management stops when the layout manager is deleted. \sa QLayoutItem, {Layout Classes}*//*! Constructs a new top-level QLayout, with parent \a parent. \a parent may not be 0. There can be only one top-level layout for a widget. It is returned by QWidget::layout().*/QLayout::QLayout(QWidget *parent) : QObject(*new QLayoutPrivate, parent){ if (!parent) return; parent->setLayout(this);}/*! Constructs a new child QLayout. This layout has to be inserted into another layout before geometry management will work.*/QLayout::QLayout() : QObject(*new QLayoutPrivate, 0){}/*! \internal */QLayout::QLayout(QLayoutPrivate &dd, QLayout *lay, QWidget *w) : QObject(dd, lay ? static_cast<QObject*>(lay) : static_cast<QObject*>(w)){ Q_D(QLayout); if (lay) { lay->addItem(this); } else if (w) { if (w->layout()) { qWarning("Attempting to add QLayout \"%s\" to %s \"%s\", which already has a" " layout.", qPrintable(QObject::objectName()), w->metaObject()->className(), w->objectName().toLocal8Bit().data()); setParent(0); } else { d->topLevel = true; w->d_func()->layout = this; invalidate(); } }}QLayoutPrivate::QLayoutPrivate() : QObjectPrivate(), insideSpacing(-1), outsideBorder(-1), topLevel(false), enabled(true), activated(true), autoNewChild(false), constraint(QLayout::SetDefaultConstraint) , menubar(0){}#ifdef QT3_SUPPORT/*! Constructs a new top-level QLayout called \a name, with parent widget \a parent. \a parent may not be 0. The \a margin is the number of pixels between the edge of the widget and the managed children. The \a spacing sets the value of spacing(), which gives the spacing between the managed widgets. If \a spacing is -1 (the default), spacing is set to the value of \a margin. There can be only one top-level layout for a widget. It is returned by QWidget::layout() \sa QWidget::setLayout()*/QLayout::QLayout(QWidget *parent, int margin, int spacing, const char *name) : QObject(*new QLayoutPrivate,parent){ Q_D(QLayout); setObjectName(QString::fromAscii(name)); d->outsideBorder = margin; if (spacing < 0) d->insideSpacing = margin; else d->insideSpacing = spacing; if (parent) { if (parent->layout()) { qWarning("QLayout \"%s\" added to %s \"%s\", which already has a layout", QObject::objectName().toLocal8Bit().data(), parent->metaObject()->className(), parent->objectName().toLocal8Bit().data()); parent->layout()->setParent(0); } else { d->topLevel = true; parent->d_func()->layout = this; invalidate(); } }}/*! Constructs a new child QLayout called \a name, and places it inside \a parentLayout by using the default placement defined by addItem(). If \a spacing is -1, this QLayout inherits \a parentLayout's spacing(), otherwise the value of \a spacing is used.*/QLayout::QLayout(QLayout *parentLayout, int spacing, const char *name) : QObject(*new QLayoutPrivate,parentLayout){ Q_D(QLayout); setObjectName(QString::fromAscii(name)); d->insideSpacing = spacing; parentLayout->addItem(this);}/*! Constructs a new child QLayout called \a name. If \a spacing is -1, this QLayout inherits its parent's spacing(); otherwise the value of \a spacing is used. This layout has to be inserted into another layout before geometry management will work.*/QLayout::QLayout(int spacing, const char *name) : QObject(*new QLayoutPrivate, 0){ Q_D(QLayout); setObjectName(QString::fromAscii(name)); d->insideSpacing = spacing;}/*! Automatically adding widgets is deprecated. Use addWidget() or addLayout() instead.*/void QLayout::setAutoAdd(bool a) { Q_D(QLayout); d->autoNewChild = a; }/*! Automatically adding widgets is deprecated. Use addWidget() or addLayout() instead.*/bool QLayout::autoAdd() const { Q_D(const QLayout); return d->autoNewChild; }#endif/*! \fn void QLayout::addItem(QLayoutItem *item) Implemented in subclasses to add an \a item. How it is added is specific to each subclass. The ownership of \a item is transferred to the layout, and it's the layout's responsibility to delete it.*//*! Adds widget \a w to this layout in a manner specific to the layout. This function uses addItem().*/void QLayout::addWidget(QWidget *w){ addChildWidget(w); addItem(new QWidgetItem(w));}/*! Sets the alignment for widget \a w to \a alignment and returns true if \a w is found in this layout (not including child layouts); otherwise returns false.*/bool QLayout::setAlignment(QWidget *w, Qt::Alignment alignment){ int i = 0; QLayoutItem *item = itemAt(i); while (item) { if (item->widget() == w) { item->setAlignment(alignment); invalidate(); return true; } ++i; item = itemAt(i); } return false;}/*! \overload Sets the alignment for the layout \a l to \a alignment and returns true if \a l is found in this layout (not including child layouts); otherwise returns false.*/bool QLayout::setAlignment(QLayout *l, Qt::Alignment alignment){ int i = 0; QLayoutItem *item = itemAt(i); while (item) { if (item->layout() == l) { item->setAlignment(alignment); invalidate(); return true; } ++i; item = itemAt(i); } return false;}/*! \fn bool QLayout::isTopLevel() const Returns true if this layout is a top-level layout, i.e. not a child of another layout; otherwise returns false.*//*! \property QLayout::margin \brief the width of the outside border of the layout The margin default is provided by the style. The default margin most Qt styles specify is 9 for child widgets and 11 for windows. \sa spacing*//*! \property QLayout::spacing \brief the spacing between widgets inside the layout The default value is -1, which signifies that the layout's spacing is inherited from the parent layout, or from the style settings for the parent widget. \sa margin*/int QLayout::margin() const{ Q_D(const QLayout); if ( d->outsideBorder >= 0 ) return d->outsideBorder; if (!d->topLevel) return 0; QWidget *pw = parentWidget(); if (pw) return pw->style()->pixelMetric( (pw->isWindow() || (pw->windowType() == Qt::SubWindow)) ? QStyle::PM_DefaultTopLevelMargin : QStyle::PM_DefaultChildMargin ); return 0;}int QLayout::spacing() const{ Q_D(const QLayout); if (d->insideSpacing >=0) { return d->insideSpacing; } else if (d->topLevel) { QWidget *pw = parentWidget(); if (pw) return pw->style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing); else return QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing); } else if (parent()) { return static_cast<QLayout*>(parent())->spacing(); } else { return -1; //this is a layout that hasn't been inserted yet }}#ifdef QT3_SUPPORTbool QLayout::isTopLevel() const{ Q_D(const QLayout); return d->topLevel;}#endifvoid QLayout::setMargin(int margin){ Q_D(QLayout); d->outsideBorder = margin; invalidate();}void QLayout::setSpacing(int spacing){ Q_D(QLayout); d->insideSpacing = spacing; invalidate();}/*! Returns the parent widget of this layout, or 0 if this layout is not installed on any widget. If the layout is a sub-layout, this function returns the parent widget of the parent layout. \sa parent()*/QWidget *QLayout::parentWidget() const{ Q_D(const QLayout); if (!d->topLevel) { if (parent()) { QLayout *parentLayout = ::qobject_cast<QLayout*>(parent()); Q_ASSERT(parentLayout); return parentLayout->parentWidget(); } else { return 0; } } else { Q_ASSERT(parent() && parent()->isWidgetType()); return static_cast<QWidget *>(parent()); }}/*! \reimp*/bool QLayout::isEmpty() const{ int i = 0; QLayoutItem *item = itemAt(i); while (item) { if (!item->isEmpty()) return false; ++i; item = itemAt(i); } return true;}/*! \reimp*/void QLayout::setGeometry(const QRect &r){ Q_D(QLayout); d->rect = r;}/*! \reimp*/QRect QLayout::geometry() const{ Q_D(const QLayout); return d->rect;}/*! \reimp*/void QLayout::invalidate(){ Q_D(QLayout); d->rect = QRect(); update();}static bool removeWidgetRecursively(QLayoutItem *li, QWidget *w){ QLayout *lay = li->layout(); if (!lay)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -