From a8cd5dc1294f2bbaa12c88d7f59b61a1011f5d18 Mon Sep 17 00:00:00 2001 From: jasplin Date: Mon, 29 Jun 2009 08:12:48 +0200 Subject: Fixed QPushButton sizeHint recalculation bug. This patch causes the size hint of QPushButton to be recalculated in cases where the value of the autoDefault property may have changed due to changes in the ancestor chain. If not explicitly set, the value of the autoDefault property depends on the presence of a QDialog ancestor. Note: The new autotest covers two different use cases related to this behavior. Reviewed-by: janarve Task-number: 255581 --- src/gui/widgets/qpushbutton.cpp | 5 +- src/gui/widgets/qpushbutton_p.h | 3 +- tests/auto/qpushbutton/tst_qpushbutton.cpp | 76 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/gui/widgets/qpushbutton.cpp b/src/gui/widgets/qpushbutton.cpp index 7da5930..a1c0d74 100644 --- a/src/gui/widgets/qpushbutton.cpp +++ b/src/gui/widgets/qpushbutton.cpp @@ -387,8 +387,9 @@ bool QPushButton::isDefault() const QSize QPushButton::sizeHint() const { Q_D(const QPushButton); - if (d->sizeHint.isValid()) + if (d->sizeHint.isValid() && d->lastAutoDefault == autoDefault()) return d->sizeHint; + d->lastAutoDefault = autoDefault(); ensurePolished(); int w = 0, h = 0; @@ -657,6 +658,8 @@ bool QPushButton::event(QEvent *e) ) { d->resetLayoutItemMargins(); updateGeometry(); + } else if (e->type() == QEvent::PolishRequest) { + updateGeometry(); } return QAbstractButton::event(e); } diff --git a/src/gui/widgets/qpushbutton_p.h b/src/gui/widgets/qpushbutton_p.h index 0c5ac79..9d682d8 100644 --- a/src/gui/widgets/qpushbutton_p.h +++ b/src/gui/widgets/qpushbutton_p.h @@ -65,7 +65,7 @@ public: QPushButtonPrivate() : QAbstractButtonPrivate(QSizePolicy::PushButton), autoDefault(Auto), - defaultButton(false), flat(false), menuOpen(false) {} + defaultButton(false), flat(false), menuOpen(false), lastAutoDefault(false) {} inline void init() { resetLayoutItemMargins(); } void resetLayoutItemMargins(); @@ -77,6 +77,7 @@ public: uint defaultButton : 1; uint flat : 1; uint menuOpen : 1; + mutable uint lastAutoDefault : 1; }; QT_END_NAMESPACE diff --git a/tests/auto/qpushbutton/tst_qpushbutton.cpp b/tests/auto/qpushbutton/tst_qpushbutton.cpp index cf79ffc..7a81dbf 100644 --- a/tests/auto/qpushbutton/tst_qpushbutton.cpp +++ b/tests/auto/qpushbutton/tst_qpushbutton.cpp @@ -51,6 +51,8 @@ #include #include #include +#include +#include Q_DECLARE_METATYPE(QPushButton*) @@ -90,6 +92,8 @@ private slots: void toggled(); void isEnabled(); void defaultAndAutoDefault(); + void sizeHint_data(); + void sizeHint(); /* void state(); void group(); @@ -590,5 +594,77 @@ void tst_QPushButton::defaultAndAutoDefault() } } +void tst_QPushButton::sizeHint_data() +{ + QTest::addColumn("stylename"); + QTest::newRow("motif") << QString::fromAscii("motif"); + QTest::newRow("cde") << QString::fromAscii("cde"); + QTest::newRow("windows") << QString::fromAscii("windows"); + QTest::newRow("cleanlooks") << QString::fromAscii("cleanlooks"); + QTest::newRow("gtk") << QString::fromAscii("gtk"); + QTest::newRow("mac") << QString::fromAscii("mac"); + QTest::newRow("plastique") << QString::fromAscii("plastique"); + QTest::newRow("windowsxp") << QString::fromAscii("windowsxp"); + QTest::newRow("windowsvista") << QString::fromAscii("windowsvista"); +} + +void tst_QPushButton::sizeHint() +{ + QFETCH(QString, stylename); + + QStyle *style = QStyleFactory::create(stylename); + if (!style) + QSKIP(qPrintable(QString::fromLatin1("Qt has been compiled without style: %1") + .arg(stylename)), SkipSingle); + QApplication::setStyle(style); + +// Test 1 + { + QPushButton *button = new QPushButton("123"); + QSize initSizeHint = button->sizeHint(); + + QDialog *dialog = new QDialog; + QWidget *widget = new QWidget(dialog); + button->setParent(widget); + button->sizeHint(); + + widget->setParent(0); + delete dialog; + button->setDefault(false); + QCOMPARE(button->sizeHint(), initSizeHint); + delete button; + } + +// Test 2 + { + QWidget *tab1 = new QWidget; + QHBoxLayout *layout1 = new QHBoxLayout(tab1); + QPushButton *button1_1 = new QPushButton("123"); + QPushButton *button1_2 = new QPushButton("123"); + layout1->addWidget(button1_1); + layout1->addWidget(button1_2); + + QWidget *tab2 = new QWidget; + QHBoxLayout *layout2 = new QHBoxLayout(tab2); + QPushButton *button2_1 = new QPushButton("123"); + QPushButton *button2_2 = new QPushButton("123"); + layout2->addWidget(button2_1); + layout2->addWidget(button2_2); + + QDialog *dialog = new QDialog; + QTabWidget *tabWidget = new QTabWidget; + tabWidget->addTab(tab1, "1"); + tabWidget->addTab(tab2, "2"); + QVBoxLayout *mainLayout = new QVBoxLayout(dialog); + mainLayout->addWidget(tabWidget); + dialog->show(); + tabWidget->setCurrentWidget(tab2); + tabWidget->setCurrentWidget(tab1); + QTest::qWait(100); + + QCOMPARE(button1_2->size(), button2_2->size()); + } +} + QTEST_MAIN(tst_QPushButton) #include "tst_qpushbutton.moc" -- cgit v0.12