diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-03-08 20:05:01 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-04-11 12:58:58 +0200 |
commit | cf5d9e9eb5c4ad77f68aa2787bfb81550ded9c23 (patch) | |
tree | 64435f40b10f072c49789612a2a70e1a7065a0a7 /src/widgets | |
parent | ce5fb1e709a7fb5594271f2aeb376fd64c5b07d8 (diff) | |
download | qtbase-cf5d9e9eb5c4ad77f68aa2787bfb81550ded9c23.tar.gz |
QWidget: add overload to set tab order as a list of widgets
The "two widgets at a time" API to set the tab order is awkward and
easily misused (as the documentation explicitly explains). Add an inline
overload that takes an initializer_list, and call the existing function
for each consecutive pair of widgets in the list.
Add documentation with snippet, and a test.
Change-Id: I8e6f14a242866e3ee7cfb8ecade4697d6bdfb4d4
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/doc/snippets/code/src_gui_kernel_qwidget.cpp | 5 | ||||
-rw-r--r-- | src/widgets/kernel/qwidget.cpp | 24 | ||||
-rw-r--r-- | src/widgets/kernel/qwidget.h | 13 |
3 files changed, 42 insertions, 0 deletions
diff --git a/src/widgets/doc/snippets/code/src_gui_kernel_qwidget.cpp b/src/widgets/doc/snippets/code/src_gui_kernel_qwidget.cpp index 8f0c296873..603ce14f80 100644 --- a/src/widgets/doc/snippets/code/src_gui_kernel_qwidget.cpp +++ b/src/widgets/doc/snippets/code/src_gui_kernel_qwidget.cpp @@ -61,6 +61,11 @@ setTabOrder(c, d); // a to b to c to d //! [9] +//! [9.list] +setTabOrder({a, b, c, d}); // a to b to c to d +//! [9.list] + + //! [10] // WRONG setTabOrder(c, d); // c to d diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp index cb432c84bc..cde0542eee 100644 --- a/src/widgets/kernel/qwidget.cpp +++ b/src/widgets/kernel/qwidget.cpp @@ -6919,6 +6919,30 @@ bool QWidget::isActiveWindow() const } /*! + \fn void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets) + \overload + \since 6.6 + + Sets the tab order for the widgets in the \a widgets list by calling + \l{QWidget::setTabOrder(QWidget *, QWidget *)} for each consecutive + pair of widgets. + + Instead of setting up each pair manually like this: + + \snippet code/src_gui_kernel_qwidget.cpp 9 + + you can call: + + \snippet code/src_gui_kernel_qwidget.cpp 9.list + + The call does not create a closed tab focus loop. If there are more widgets + with \l{Qt::TabFocus} focus policy, tabbing on \c{d} will move focus to one + of those widgets, not back to \c{a}. + + \sa setFocusPolicy(), setFocusProxy(), {Keyboard Focus in Widgets} +*/ + +/*! Puts the \a second widget after the \a first widget in the focus order. It effectively removes the \a second widget from its focus chain and diff --git a/src/widgets/kernel/qwidget.h b/src/widgets/kernel/qwidget.h index a0a1ca5300..a17803cabc 100644 --- a/src/widgets/kernel/qwidget.h +++ b/src/widgets/kernel/qwidget.h @@ -432,6 +432,7 @@ public: void setFocusPolicy(Qt::FocusPolicy policy); bool hasFocus() const; static void setTabOrder(QWidget *, QWidget *); + static inline void setTabOrder(std::initializer_list<QWidget *> widgets); void setFocusProxy(QWidget *); QWidget *focusProxy() const; Qt::ContextMenuPolicy contextMenuPolicy() const; @@ -916,6 +917,18 @@ inline bool QWidget::testAttribute(Qt::WidgetAttribute attribute) const return testAttribute_helper(attribute); } +inline void QWidget::setTabOrder(std::initializer_list<QWidget *> widgets) +{ + QWidget *prev = nullptr; + for (const auto &widget : widgets) { + if (!prev) { + prev = widget; + } else { + QWidget::setTabOrder(prev, widget); + prev = widget; + } + } +} #define QWIDGETSIZE_MAX ((1<<24)-1) |