int hfw = 0;
for (int i = 0; i < n; ++i) {
if (QLayoutItem *item = itemAt(i)) {
- hfw = qMax(hfw, item->heightForWidth(width));
+ if (QWidget *w = item->widget())
+ /*
+ Note: Does not query the layout item, but bypasses it and asks the widget
+ directly. This is consistent with how QStackedLayout::sizeHint() is
+ implemented. This also avoids an issue where QWidgetItem::heightForWidth()
+ returns -1 if the widget is hidden.
+ */
+ hfw = qMax(hfw, w->heightForWidth(width));
}
}
+ hfw = qMax(hfw, minimumSize().height());
return hfw;
}
#include <QtTest/QtTest>
#include <QLineEdit>
+#include <QLabel>
#include <QStackedLayout>
#include <qapplication.h>
#include <qwidget.h>
void deleteCurrent();
void removeWidget();
void keepFocusAfterSetCurrent();
+ void heigthForWidth();
private:
QWidget *testWidget;
QVERIFY(edit2->hasFakeEditFocus);
}
+void tst_QStackedLayout::heigthForWidth()
+{
+ if (testWidget->layout()) delete testWidget->layout();
+ QStackedLayout *stackLayout = new QStackedLayout(testWidget);
+
+ QLabel *shortLabel = new QLabel("This is a short text.");
+ shortLabel->setWordWrap(true);
+ stackLayout->addWidget(shortLabel);
+
+ QLabel *longLabel = new QLabel("Write code once to target multiple platforms\n"
+ "Qt allows you to write advanced applications and UIs once, "
+ "and deploy them across desktop and embedded operating systems "
+ "without rewriting the source code saving time and development cost.\n\n"
+ "Create amazing user experiences\n"
+ "Whether you prefer C++ or JavaScript, Qt provides the building blocks - "
+ "a broad set of customizable widgets, graphics canvas, style engine "
+ "and more that you need to build modern user interfaces. "
+ "Incorporate 3D graphics, multimedia audio or video, visual effects, "
+ "and animations to set your application apart from the competition.");
+
+ longLabel->setWordWrap(true);
+ stackLayout->addWidget(longLabel);
+ stackLayout->setCurrentIndex(0);
+ int hfw_index0 = stackLayout->heightForWidth(200);
+
+ stackLayout->setCurrentIndex(1);
+ QCOMPARE(stackLayout->heightForWidth(200), hfw_index0);
+
+}
+
QTEST_MAIN(tst_QStackedLayout)
#include "tst_qstackedlayout.moc"