Implements QStackedLayout's hfw-related methods.

QStackedLayout does not support height for width (simply because it does
not reimplement heightForWidth() and hasHeightForWidth()). That is not
possible to fix without breaking binary compatibility under Qt4, which
use a modified version of QStackedLayout that reimplements the hfw-related
functions as a workaround.

Change-Id: I81c795f0c247a2e708292de35f0650384248c6cd
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
This commit is contained in:
Debao Zhang 2012-03-02 15:42:35 -08:00 committed by Qt by Nokia
parent fb8c95bac0
commit 07ae18f96e
3 changed files with 36 additions and 44 deletions

View File

@ -476,6 +476,38 @@ void QStackedLayout::setGeometry(const QRect &rect)
}
}
/*!
\reimp
*/
bool QStackedLayout::hasHeightForWidth() const
{
const int n = count();
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
if (item->hasHeightForWidth())
return true;
}
}
return false;
}
/*!
\reimp
*/
int QStackedLayout::heightForWidth(int width) const
{
const int n = count();
int hfw = 0;
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
hfw = qMax(hfw, item->heightForWidth(width));
}
}
return hfw;
}
/*!
\enum QStackedLayout::StackingMode
\since 4.4

View File

@ -94,6 +94,8 @@ public:
QLayoutItem *itemAt(int) const;
QLayoutItem *takeAt(int);
void setGeometry(const QRect &rect);
bool hasHeightForWidth() const;
int heightForWidth(int width) const;
Q_SIGNALS:
void widgetRemoved(int index);

View File

@ -49,54 +49,12 @@
QT_BEGIN_NAMESPACE
/**
QStackedLayout does not support height for width (simply because it does not reimplement
heightForWidth() and hasHeightForWidth()). That is not possible to fix without breaking
binary compatibility. (QLayout is subject to multiple inheritance).
However, we can fix QStackedWidget by simply using a modified version of QStackedLayout
that reimplements the hfw-related functions:
*/
class QStackedLayoutHFW : public QStackedLayout
{
public:
QStackedLayoutHFW(QWidget *parent = 0) : QStackedLayout(parent) {}
bool hasHeightForWidth() const;
int heightForWidth(int width) const;
};
bool QStackedLayoutHFW::hasHeightForWidth() const
{
const int n = count();
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
if (item->hasHeightForWidth())
return true;
}
}
return false;
}
int QStackedLayoutHFW::heightForWidth(int width) const
{
const int n = count();
int hfw = 0;
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
hfw = qMax(hfw, item->heightForWidth(width));
}
}
return hfw;
}
class QStackedWidgetPrivate : public QFramePrivate
{
Q_DECLARE_PUBLIC(QStackedWidget)
public:
QStackedWidgetPrivate():layout(0){}
QStackedLayoutHFW *layout;
QStackedLayout *layout;
bool blockChildAdd;
};
@ -180,7 +138,7 @@ QStackedWidget::QStackedWidget(QWidget *parent)
: QFrame(*new QStackedWidgetPrivate, parent)
{
Q_D(QStackedWidget);
d->layout = new QStackedLayoutHFW(this);
d->layout = new QStackedLayout(this);
connect(d->layout, SIGNAL(widgetRemoved(int)), this, SIGNAL(widgetRemoved(int)));
connect(d->layout, SIGNAL(currentChanged(int)), this, SIGNAL(currentChanged(int)));
}