diff options
author | Jason Barron <jbarron@trolltech.com> | 2009-11-24 14:52:38 (GMT) |
---|---|---|
committer | Jason Barron <jbarron@trolltech.com> | 2009-11-24 14:56:43 (GMT) |
commit | a0c8e134a284d45520dd3a229e68dbcd155299e6 (patch) | |
tree | 15f1f4423a4dac17c57f9810f6ac280d85583f89 | |
parent | 4817debee23a72f44eedbb8f33b6b611b5161174 (diff) | |
download | Qt-a0c8e134a284d45520dd3a229e68dbcd155299e6.zip Qt-a0c8e134a284d45520dd3a229e68dbcd155299e6.tar.gz Qt-a0c8e134a284d45520dd3a229e68dbcd155299e6.tar.bz2 |
Fix softkeys for QDialogButtonBoxes created without a parent.
Inthe case where a QDialogButtonBox was created without a parent, for
example in the FTP example, the softkeys that are automatically created
inside the button box were not being added to the right widget when the
button box was given a parent (or added to a layout) later.
This patch resolves that issue by handling the ParentChange event and
then adding the softkeys at this point.
Task-number: QTBUG-6086
Reviewed-by: axis
-rw-r--r-- | src/gui/widgets/qdialogbuttonbox.cpp | 24 | ||||
-rw-r--r-- | tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp | 60 |
2 files changed, 68 insertions, 16 deletions
diff --git a/src/gui/widgets/qdialogbuttonbox.cpp b/src/gui/widgets/qdialogbuttonbox.cpp index 2ee5751..0e859f1 100644 --- a/src/gui/widgets/qdialogbuttonbox.cpp +++ b/src/gui/widgets/qdialogbuttonbox.cpp @@ -1215,6 +1215,30 @@ bool QDialogButtonBox::event(QEvent *event) }else if (event->type() == QEvent::LanguageChange) { d->retranslateStrings(); } +#ifdef QT_SOFTKEYS_ENABLED + else if (event->type() == QEvent::ParentChange) { + QWidget *dialog = 0; + QWidget *p = this; + while (p && !p->isWindow()) { + p = p->parentWidget(); + if ((dialog = qobject_cast<QDialog *>(p))) + break; + } + + // If the parent changes, then move the softkeys + for (QHash<QAbstractButton *, QAction *>::const_iterator it = d->softKeyActions.constBegin(); + it != d->softKeyActions.constEnd(); ++it) { + QAction *current = it.value(); + QList<QWidget *> widgets = current->associatedWidgets(); + foreach (QWidget *w, widgets) + w->removeAction(current); + if (dialog) + dialog->addAction(current); + else + addAction(current); + } + } +#endif return QWidget::event(event); } diff --git a/tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp b/tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp index 936ebf7..2c49fc8 100644 --- a/tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp +++ b/tests/auto/qdialogbuttonbox/tst_qdialogbuttonbox.cpp @@ -111,6 +111,9 @@ private slots: void testDefaultButton_data(); void testDefaultButton(); void testS60SoftKeys(); +#ifdef QT_SOFTKEYS_ENABLED + void testSoftKeyReparenting(); +#endif void task191642_default(); private: @@ -715,6 +718,17 @@ void tst_QDialogButtonBox::testDefaultButton_data() QTest::newRow("third accept explicit after add") << 0 << 2 << 2; } +static int softKeyCount(QWidget *widget) +{ + int softkeyCount = 0; + QList<QAction *> actions = widget->actions(); + foreach (QAction *action, actions) { + if (action->softKeyRole() != QAction::NoSoftKey) + softkeyCount++; + } + return softkeyCount; +} + void tst_QDialogButtonBox::testS60SoftKeys() { #ifdef Q_WS_S60 @@ -722,33 +736,47 @@ void tst_QDialogButtonBox::testS60SoftKeys() QDialogButtonBox buttonBox(&dialog); buttonBox.setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); dialog.show(); - - int softkeyCount = 0; - QList<QAction *> actions = dialog.actions(); - foreach (QAction *action, actions) { - if (action->softKeyRole() != QAction::NoSoftKey) - softkeyCount++; - } - QCOMPARE( softkeyCount, 2); + + QCOMPARE( softKeyCount(&dialog), 2); QDialog dialog2(0); QDialogButtonBox buttonBox2(&dialog2); buttonBox2.setStandardButtons(QDialogButtonBox::Cancel); dialog2.show(); - int softkeyCount2 = 0; - QList<QAction *> actions2 = dialog2.actions(); - foreach (QAction *action, actions2) { - if (action->softKeyRole() != QAction::NoSoftKey) - softkeyCount2++; - } - QCOMPARE( softkeyCount2, 1); - + QCOMPARE( softKeyCount(&dialog2), 1); + #else QSKIP("S60-specific test", SkipAll ); #endif } +#ifdef QT_SOFTKEYS_ENABLED +void tst_QDialogButtonBox::testSoftKeyReparenting() +{ + QDialog dialog; + QDialogButtonBox *buttonBox = new QDialogButtonBox; + buttonBox->addButton(QDialogButtonBox::Ok); + buttonBox->addButton(QDialogButtonBox::Cancel); + + QCOMPARE(softKeyCount(&dialog), 0); + QCOMPARE(softKeyCount(buttonBox), 2); + + // Were the softkeys re-parented correctly? + dialog.setLayout(new QVBoxLayout); + dialog.layout()->addWidget(buttonBox); + QCOMPARE(softKeyCount(&dialog), 2); + QCOMPARE(softKeyCount(buttonBox), 0); + + // Softkeys are only added to QDialog, not QWidget + QWidget *nested = new QWidget; + nested->setLayout(new QVBoxLayout); + nested->layout()->addWidget(buttonBox); + QCOMPARE(softKeyCount(nested), 0); + QCOMPARE(softKeyCount(buttonBox), 2); +} +#endif + void tst_QDialogButtonBox::testDefaultButton() { QFETCH(int, whenToSetDefault); |