From 63b574ee1539b34ac7df890a7941320deb1c258e Mon Sep 17 00:00:00 2001 From: Alexis Menard Date: Tue, 20 Oct 2009 11:52:38 +0200 Subject: Fix bug in embedded dialog demo with tab focus. On embedded dialog pressing tab stop changing the focus when the focus was given to QFontComboBox. It's because QFontComboBox embed a QLineEdit in order to allow editing. But this QLineEdit is a focus proxy so we need to special case that. The logic is the same in QApplication. Be careful when changing one of them. Task-number:QTBUG-4818 Reviewed-by:jan-arve Reviewed-by:ogoffart --- src/gui/graphicsview/qgraphicsproxywidget.cpp | 15 ++++++-- src/gui/kernel/qapplication.cpp | 1 + .../tst_qgraphicsproxywidget.cpp | 45 +++++++++++++++++++++- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsproxywidget.cpp b/src/gui/graphicsview/qgraphicsproxywidget.cpp index b7a3962..64c51ad 100644 --- a/src/gui/graphicsview/qgraphicsproxywidget.cpp +++ b/src/gui/graphicsview/qgraphicsproxywidget.cpp @@ -57,6 +57,9 @@ #include #include #include +#include +#include +#include QT_BEGIN_NAMESPACE @@ -86,7 +89,9 @@ QT_BEGIN_NAMESPACE of embedded widgets through creating a child proxy for each popup. This means that when an embedded QComboBox shows its popup list, a new QGraphicsProxyWidget is created automatically, embedding the popup, and - positioning it correctly. + positioning it correctly. This only works if the popup is child of the + embedded widget (for example QToolButton::setMenu() requires the QMenu instance + to be child of the QToolButton). \section1 Embedding a Widget with QGraphicsProxyWidget @@ -184,6 +189,7 @@ QT_BEGIN_NAMESPACE */ extern bool qt_sendSpontaneousEvent(QObject *, QEvent *); +extern bool qt_tab_all_widgets; /*! \internal @@ -369,6 +375,7 @@ QVariant QGraphicsProxyWidgetPrivate::inputMethodQueryHelper(Qt::InputMethodQuer /*! \internal + Some of the logic is shared with QApplicationPrivate::focusNextPrevChild_helper */ QWidget *QGraphicsProxyWidgetPrivate::findFocusChild(QWidget *child, bool next) const { @@ -382,14 +389,16 @@ QWidget *QGraphicsProxyWidgetPrivate::findFocusChild(QWidget *child, bool next) child = next ? child->d_func()->focus_next : child->d_func()->focus_prev; if ((next && child == widget) || (!next && child == widget->d_func()->focus_prev)) { return 0; - } + } } QWidget *oldChild = child; + uint focus_flag = qt_tab_all_widgets ? Qt::TabFocus : Qt::StrongFocus; do { if (child->isEnabled() && child->isVisibleTo(widget) - && (child->focusPolicy() & Qt::TabFocus)) { + && (child->focusPolicy() & focus_flag == focus_flag) + && !(child->d_func()->extra && child->d_func()->extra->focus_proxy)) { return child; } child = next ? child->d_func()->focus_next : child->d_func()->focus_prev; diff --git a/src/gui/kernel/qapplication.cpp b/src/gui/kernel/qapplication.cpp index f48c551..c4249d9 100644 --- a/src/gui/kernel/qapplication.cpp +++ b/src/gui/kernel/qapplication.cpp @@ -2495,6 +2495,7 @@ void QApplication::setActiveWindow(QWidget* act) /*!internal * Helper function that returns the new focus widget, but does not set the focus reason. * Returns 0 if a new focus widget could not be found. + * Shared with QGraphicsProxyWidgetPrivate::findFocusChild() */ QWidget *QApplicationPrivate::focusNextPrevChild_helper(QWidget *toplevel, bool next) { diff --git a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp index 2426ce9..9269164 100644 --- a/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp +++ b/tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp @@ -2007,8 +2007,10 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() edit1->setText("QLineEdit 1"); QLineEdit *edit2 = new QLineEdit; edit2->setText("QLineEdit 2"); + QFontComboBox *fontComboBox = new QFontComboBox; QVBoxLayout *vlayout = new QVBoxLayout; vlayout->addWidget(edit1); + vlayout->addWidget(fontComboBox); vlayout->addWidget(edit2); QGroupBox *box = new QGroupBox("QGroupBox"); @@ -2020,8 +2022,10 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() edit1_2->setText("QLineEdit 1_2"); QLineEdit *edit2_2 = new QLineEdit; edit2_2->setText("QLineEdit 2_2"); + QFontComboBox *fontComboBox2 = new QFontComboBox; vlayout = new QVBoxLayout; vlayout->addWidget(edit1_2); + vlayout->addWidget(fontComboBox2); vlayout->addWidget(edit2_2); QGroupBox *box_2 = new QGroupBox("QGroupBox 2"); @@ -2062,8 +2066,10 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() EventSpy eventSpy(edit1); EventSpy eventSpy2(edit2); + EventSpy eventSpy3(fontComboBox); EventSpy eventSpy1_2(edit1_2); EventSpy eventSpy2_2(edit2_2); + EventSpy eventSpy2_3(fontComboBox2); EventSpy eventSpyBox(box); // Tab into group box @@ -2084,11 +2090,24 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() QCOMPARE(eventSpy.counts[QEvent::FocusIn], 1); QCOMPARE(eventSpy.counts[QEvent::FocusOut], 0); + // Tab to the font combobox + QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab); + QApplication::processEvents(); + fontComboBox->hasFocus(); + QVERIFY(!edit2->hasFocus()); + QCOMPARE(eventSpy3.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy3.counts[QEvent::FocusOut], 0); + QCOMPARE(eventSpy.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy.counts[QEvent::FocusOut], 1); + // Tab into line edit 2 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab); QApplication::processEvents(); edit2->hasFocus(); QVERIFY(!edit1->hasFocus()); + QCOMPARE(eventSpy2.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy2.counts[QEvent::FocusOut], 0); + QCOMPARE(eventSpy3.counts[QEvent::FocusOut], 1); QCOMPARE(eventSpy.counts[QEvent::FocusIn], 1); QCOMPARE(eventSpy.counts[QEvent::FocusOut], 1); @@ -2106,6 +2125,16 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() QCOMPARE(eventSpy1_2.counts[QEvent::FocusIn], 1); QCOMPARE(eventSpy1_2.counts[QEvent::FocusOut], 0); + // Tab into right font combobox + QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab); + QApplication::processEvents(); + QVERIFY(!edit1_2->hasFocus()); + fontComboBox2->hasFocus(); + QCOMPARE(eventSpy1_2.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy1_2.counts[QEvent::FocusOut], 1); + QCOMPARE(eventSpy2_3.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy2_3.counts[QEvent::FocusOut], 0); + // Tab into right bottom line edit QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab); QApplication::processEvents(); @@ -2113,6 +2142,8 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() edit2_2->hasFocus(); QCOMPARE(eventSpy1_2.counts[QEvent::FocusIn], 1); QCOMPARE(eventSpy1_2.counts[QEvent::FocusOut], 1); + QCOMPARE(eventSpy2_3.counts[QEvent::FocusIn], 1); + QCOMPARE(eventSpy2_3.counts[QEvent::FocusOut], 1); QCOMPARE(eventSpy2_2.counts[QEvent::FocusIn], 1); QCOMPARE(eventSpy2_2.counts[QEvent::FocusOut], 0); @@ -2129,6 +2160,12 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() QVERIFY(!rightDial->hasFocus()); edit2_2->hasFocus(); + // Backtab into the right font combobox + QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab); + QApplication::processEvents(); + QVERIFY(!edit2_2->hasFocus()); + fontComboBox2->hasFocus(); + // Backtab into line edit 1 QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab); QApplication::processEvents(); @@ -2147,10 +2184,16 @@ void tst_QGraphicsProxyWidget::tabFocus_complexTwoWidgets() QVERIFY(!rightDial->hasFocus()); edit2->hasFocus(); - // Backtab into line edit 1 + // Backtab into the font combobox QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab); QApplication::processEvents(); QVERIFY(!edit2->hasFocus()); + fontComboBox->hasFocus(); + + // Backtab into line edit 1 + QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab); + QApplication::processEvents(); + QVERIFY(!fontComboBox->hasFocus()); edit1->hasFocus(); // Backtab into line box -- cgit v0.12