QWidget: copy Q{Menu,ToolBar}::addActions() functions

Since any QWidget can have actions since at least Qt 5.0, it makes no
sense to keep the addActions(text) etc. convenience functions that
have traditionally existed in QMenu and QToolBar only in these two
classes, where, to add insult to injury, they were just copies of each
other, increasing library size for no real reason.

So, add them to QWidget, too. This will allow us to de-duplicate the
code between QMenu and QToolBar, too, which will be done in a
follow-up patch, subject to BC constraints.

[ChangeLog][QtWidgets][QWidget] Added the same addAction(text)
overloads that previously existed only on QMenu and QToolBar, with the
following two differences: First, the QKeySequence object, if any, is
now available on all overloads, not just the ones taking a slot, but
has changed position to allow, secondly, passing an optional
Qt::ConnectionType parameter which will be passed to the
QObject::connect() call. In addition, the function template overloads
are now properly constrained so that they exist if and only if the
corresponding QObject::connect() call does, too.

Change-Id: Ifc3c2789600cf603192de8224fecbf9c88d91970
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2021-07-08 16:14:50 +02:00
parent 85d27945ac
commit 08e4d2db08
2 changed files with 176 additions and 1 deletions

View File

@ -3207,6 +3207,127 @@ QList<QAction*> QWidget::actions() const
Q_D(const QWidget);
return d->actions;
}
/*!
\fn QAction *QWidget::addAction(const QString &text);
\fn QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut);
\fn QAction *QWidget::addAction(const QIcon &icon, const QString &text);
\fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut);
\since 6.3
These convenience functions create a new action with text \a text,
icon \a icon and shortcut \a shortcut, if any.
The functions add the newly created action to the widget's
list of actions, and return it.
QWidget takes ownership of the returned QAction.
*/
QAction *QWidget::addAction(const QString &text)
{
QAction *ret = new QAction(text, this);
addAction(ret);
return ret;
}
QAction *QWidget::addAction(const QIcon &icon, const QString &text)
{
QAction *ret = new QAction(icon, text, this);
addAction(ret);
return ret;
}
QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut)
{
QAction *ret = addAction(text);
ret->setShortcut(shortcut);
return ret;
}
QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut)
{
QAction *ret = addAction(icon, text);
ret->setShortcut(shortcut);
return ret;
}
/*!
\fn QAction *QWidget::addAction(const QString &text, const QObject *receiver, const char* member, Qt::ConnectionType type)
\fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, Qt::ConnectionType type)
\fn QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char* member, Qt::ConnectionType type)
\fn QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char* member, Qt::ConnectionType type)
\overload
\since 6.3
This convenience function creates a new action with the text \a
text, icon \a icon, and shortcut \a shortcut, if any.
The action's \l{QAction::triggered()}{triggered()} signal is connected
to the \a receiver's \a member slot. The function adds the newly created
action to the widget's list of actions and returns it.
QWidget takes ownership of the returned QAction.
*/
QAction *QWidget::addAction(const QString &text, const QObject *receiver, const char* member,
Qt::ConnectionType type)
{
QAction *action = addAction(text);
QObject::connect(action, SIGNAL(triggered(bool)), receiver, member, type);
return action;
}
QAction *QWidget::addAction(const QIcon &icon, const QString &text,
const QObject *receiver, const char* member,
Qt::ConnectionType type)
{
QAction *action = addAction(icon, text);
QObject::connect(action, SIGNAL(triggered(bool)), receiver, member, type);
return action;
}
#if QT_CONFIG(shortcut)
QAction *QWidget::addAction(const QString &text, const QKeySequence &shortcut,
const QObject *receiver, const char* member,
Qt::ConnectionType type)
{
QAction *action = addAction(text, receiver, member, type);
action->setShortcut(shortcut);
return action;
}
QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut,
const QObject *receiver, const char* member,
Qt::ConnectionType type)
{
QAction *action = addAction(icon, text, receiver, member, type);
action->setShortcut(shortcut);
return action;
}
#endif // QT_CONFIG(shortcut)
/*!
\fn template<typename...Args> QAction *QWidget::addAction(const QString &text, Args&&...args)
\fn template<typename...Args> QAction *QWidget::addAction(const QString &text, const QShortcut &shortcut, Args&&...args)
\fn template<typename...Args> QAction *QWidget::addAction(const QIcon &icon, const QString &text, Args&&...args)
\fn template<typename...Args> QAction *QWidget::addAction(const QIcon &icon, const QString &text, const QShortcut &shortcut, Args&&...args)
\since 6.3
\overload
These convenience functions create a new action with the text \a text,
icon \a icon, and shortcut \a shortcut, if any.
The action's \l{QAction::triggered()}{triggered()} signal is connected
as if by a call to QObject::connect(action, &QAction::triggered, args...),
perfectly forwarding \a args, including a possible Qt::ConnectionType.
The function adds the newly created action to the widget's list of
actions and returns it.
QWidget takes ownership of the returned QAction.
*/
#endif // QT_NO_ACTION
/*!

View File

@ -44,6 +44,7 @@
#include <QtGui/qwindowdefs.h>
#include <QtCore/qobject.h>
#include <QtCore/qmargins.h>
#include <QtGui/qaction.h>
#include <QtGui/qpaintdevice.h>
#include <QtGui/qpalette.h>
#include <QtGui/qfont.h>
@ -210,6 +211,11 @@ class Q_WIDGETS_EXPORT QWidget : public QObject, public QPaintDevice
Q_PROPERTY(QString windowFilePath READ windowFilePath WRITE setWindowFilePath)
Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints)
template <typename...Args>
using compatible_action_slot_args = std::void_t<
decltype(QObject::connect(std::declval<QAction*>(), &QAction::triggered,
std::declval<Args>()...))
>;
public:
enum RenderFlag {
DrawWindowBackground = 0x1,
@ -569,7 +575,55 @@ public:
void insertAction(QAction *before, QAction *action);
void removeAction(QAction *action);
QList<QAction*> actions() const;
#endif
// convenience action factories
QAction *addAction(const QString &text);
QAction *addAction(const QIcon &icon, const QString &text);
QAction *addAction(const QString &text, const QObject *receiver,
const char *member, Qt::ConnectionType type = Qt::AutoConnection);
QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver,
const char *member, Qt::ConnectionType type = Qt::AutoConnection);
template <typename...Args, typename = compatible_action_slot_args<Args...>>
QAction *addAction(const QString &text, Args&&...args)
{
QAction *result = addAction(text);
connect(result, &QAction::triggered, std::forward<Args>(args)...);
return result;
}
template <typename...Args, typename = compatible_action_slot_args<Args...>>
QAction *addAction(const QIcon &icon, const QString &text, Args&&...args)
{
QAction *result = addAction(icon, text);
connect(result, &QAction::triggered, std::forward<Args>(args)...);
return result;
}
#if QT_CONFIG(shortcut)
QAction *addAction(const QString &text, const QKeySequence &shortcut);
QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut);
QAction *addAction(const QString &text, const QKeySequence &shortcut,
const QObject *receiver, const char *member,
Qt::ConnectionType type = Qt::AutoConnection);
QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut,
const QObject *receiver, const char *member,
Qt::ConnectionType type = Qt::AutoConnection);
template <typename...Args, typename = compatible_action_slot_args<Args...>>
QAction *addAction(const QString &text, const QKeySequence &shortcut, Args&&...args)
{
QAction *result = addAction(text, shortcut);
connect(result, &QAction::triggered, std::forward<Args>(args)...);
return result;
}
template <typename...Args, typename = compatible_action_slot_args<Args...>>
QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, Args&&...args)
{
QAction *result = addAction(icon, text, shortcut);
connect(result, &QAction::triggered, std::forward<Args>(args)...);
return result;
}
#endif // QT_CONFIG(shortcut)
#endif // QT_NO_ACTION
QWidget *parentWidget() const;