summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-06-17 12:21:20 +0200
committerFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-06-18 14:42:24 +0000
commitc0eafb9d75b6e24c03cde848b6482d0fcad9ee31 (patch)
treea7b7cf882659704848dd7061d067c8cb11929e54
parenta851868e63311622bb83d2e08388b34c4d6d869e (diff)
QMenu/QToolBar: Add overloads of addAction() using Qt 5 signals and slots.
Add documentation dummies and templates, add tests verifying compilation. Change-Id: Ide336b28bc069cfd17848ce3a17fd428e36ed65b Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
-rw-r--r--src/widgets/widgets/qmenu.cpp94
-rw-r--r--src/widgets/widgets/qmenu.h66
-rw-r--r--src/widgets/widgets/qtoolbar.cpp76
-rw-r--r--src/widgets/widgets/qtoolbar.h46
-rw-r--r--tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp29
-rw-r--r--tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp18
6 files changed, 329 insertions, 0 deletions
diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp
index 4403deda8e..bb2ffb63b8 100644
--- a/src/widgets/widgets/qmenu.cpp
+++ b/src/widgets/widgets/qmenu.cpp
@@ -1571,6 +1571,100 @@ QAction *QMenu::addAction(const QString &text, const QObject *receiver, const ch
return action;
}
+/*!\fn QAction *QMenu::addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with the text \a
+ text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a method of the \a receiver. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
+/*!\fn QAction *QMenu::addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with the text \a
+ text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
+/*!\fn QAction *QMenu::addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with the text \a
+ text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ If \a context is destroyed, the functor will not be called.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
+/*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with an \a icon
+ and some \a text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a method of the \a receiver. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
+/*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with an \a icon
+ and some \a text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
+/*!\fn QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0)
+
+ \since 5.6
+
+ \overload
+
+ This convenience function creates a new action with an \a icon
+ and some \a text and an optional shortcut \a shortcut. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor. The function adds the newly created
+ action to the menu's list of actions and returns it.
+
+ If \a context is destroyed, the functor will not be called.
+
+ QMenu takes ownership of the returned QAction.
+*/
+
/*!
\overload
diff --git a/src/widgets/widgets/qmenu.h b/src/widgets/widgets/qmenu.h
index 5dda8b2370..21e1432d2a 100644
--- a/src/widgets/widgets/qmenu.h
+++ b/src/widgets/widgets/qmenu.h
@@ -78,6 +78,72 @@ public:
QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
+#ifdef Q_QDOC
+ QAction *addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0);
+ QAction *addAction(const QString &text, Functor functor, const QKeySequence &shortcut = 0);
+ QAction *addAction(const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
+ QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0);
+ QAction *addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut = 0);
+ QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor, const QKeySequence &shortcut = 0);
+#else
+ // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
+ template<class Obj, typename Func1>
+ inline typename QtPrivate::QEnableIf<!QtPrivate::is_same<const char*, Func1>::value
+ && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::Type
+ addAction(const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
+ {
+ QAction *result = addAction(text);
+#ifdef QT_NO_SHORTCUT
+ Q_UNUSED(shortcut)
+#else
+ result->setShortcut(shortcut);
+#endif
+ connect(result, &QAction::triggered, object, slot);
+ return result;
+ }
+ // addAction(QString): Connect to a functor or function pointer (without context)
+ template <typename Func1>
+ inline QAction *addAction(const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
+ {
+ QAction *result = addAction(text);
+#ifdef QT_NO_SHORTCUT
+ Q_UNUSED(shortcut)
+#else
+ result->setShortcut(shortcut);
+#endif
+ connect(result, &QAction::triggered, slot);
+ return result;
+ }
+ // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context)
+ template<class Obj, typename Func1>
+ inline typename QtPrivate::QEnableIf<!QtPrivate::is_same<const char*, Func1>::value
+ && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::Type
+ addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot, const QKeySequence &shortcut = 0)
+ {
+ QAction *result = addAction(actionIcon, text);
+#ifdef QT_NO_SHORTCUT
+ Q_UNUSED(shortcut)
+#else
+ result->setShortcut(shortcut);
+#endif
+ connect(result, &QAction::triggered, object, slot);
+ return result;
+ }
+ // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
+ template <typename Func1>
+ inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot, const QKeySequence &shortcut = 0)
+ {
+ QAction *result = addAction(actionIcon, text);
+#ifdef QT_NO_SHORTCUT
+ Q_UNUSED(shortcut)
+#else
+ result->setShortcut(shortcut);
+#endif
+ connect(result, &QAction::triggered, slot);
+ return result;
+ }
+#endif // !Q_QDOC
+
QAction *addMenu(QMenu *menu);
QMenu *addMenu(const QString &title);
QMenu *addMenu(const QIcon &icon, const QString &title);
diff --git a/src/widgets/widgets/qtoolbar.cpp b/src/widgets/widgets/qtoolbar.cpp
index 08dea699e0..89a82edaae 100644
--- a/src/widgets/widgets/qtoolbar.cpp
+++ b/src/widgets/widgets/qtoolbar.cpp
@@ -793,6 +793,82 @@ QAction *QToolBar::addAction(const QIcon &icon, const QString &text,
return action;
}
+/*!\fn QAction *QToolBar::addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a text. This action is added to
+ the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a method of the \a receiver.
+*/
+
+/*!\fn QAction *QToolBar::addAction(const QString &text, Functor functor)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a text. This action is added to
+ the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor.
+*/
+
+/*!\fn QAction *QToolBar::addAction(const QString &text, const QObject *context, Functor functor)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a text. This action is added to
+ the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor.
+
+ If \a context is destroyed, the functor will not be called.
+*/
+
+/*!\fn QAction *QToolBar::addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a icon and \a text. This
+ action is added to the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a method of the \a receiver.
+*/
+
+/*!\fn QAction *QToolBar::addAction(const QIcon &icon, const QString &text, Functor functor)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a icon and \a text. This
+ action is added to the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor.
+*/
+
+/*!\fn QAction *QToolBar::addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor)
+
+ \since 5.6
+
+ \overload
+
+ Creates a new action with the given \a icon and \a text. This
+ action is added to the end of the toolbar. The action's
+ \l{QAction::triggered()}{triggered()} signal is connected to the
+ \a functor.
+
+ If \a context is destroyed, the functor will not be called.
+*/
+
/*!
Adds a separator to the end of the toolbar.
diff --git a/src/widgets/widgets/qtoolbar.h b/src/widgets/widgets/qtoolbar.h
index f925577646..f4ed13d399 100644
--- a/src/widgets/widgets/qtoolbar.h
+++ b/src/widgets/widgets/qtoolbar.h
@@ -34,6 +34,7 @@
#ifndef QDYNAMICTOOLBAR_H
#define QDYNAMICTOOLBAR_H
+#include <QtWidgets/qaction.h>
#include <QtWidgets/qwidget.h>
QT_BEGIN_NAMESPACE
@@ -92,6 +93,51 @@ public:
QAction *addAction(const QString &text, const QObject *receiver, const char* member);
QAction *addAction(const QIcon &icon, const QString &text,
const QObject *receiver, const char* member);
+#ifdef Q_QDOC
+ QAction *addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method);
+ QAction *addAction(const QString &text, Functor functor);
+ QAction *addAction(const QString &text, const QObject *context, Functor functor);
+ QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method);
+ QAction *addAction(const QIcon &icon, const QString &text, Functor functor);
+ QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor);
+#else
+ // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
+ template<class Obj, typename Func1>
+ inline typename QtPrivate::QEnableIf<!QtPrivate::is_same<const char*, Func1>::value
+ && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::Type
+ addAction(const QString &text, const Obj *object, Func1 slot)
+ {
+ QAction *result = addAction(text);
+ connect(result, &QAction::triggered, object, slot);
+ return result;
+ }
+ // addAction(QString): Connect to a functor or function pointer (without context)
+ template <typename Func1>
+ inline QAction *addAction(const QString &text, Func1 slot)
+ {
+ QAction *result = addAction(text);
+ connect(result, &QAction::triggered, slot);
+ return result;
+ }
+ // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
+ template<class Obj, typename Func1>
+ inline typename QtPrivate::QEnableIf<!QtPrivate::is_same<const char*, Func1>::value
+ && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::Type
+ addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot)
+ {
+ QAction *result = addAction(actionIcon, text);
+ connect(result, &QAction::triggered, object, slot);
+ return result;
+ }
+ // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
+ template <typename Func1>
+ inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot)
+ {
+ QAction *result = addAction(actionIcon, text);
+ connect(result, &QAction::triggered, slot);
+ return result;
+ }
+#endif // !Q_QDOC
QAction *addSeparator();
QAction *insertSeparator(QAction *before);
diff --git a/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp b/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp
index 0c06d72026..7e0e752f02 100644
--- a/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp
+++ b/tests/auto/widgets/widgets/qmenu/tst_qmenu.cpp
@@ -78,6 +78,7 @@ public slots:
private slots:
void getSetCheck();
void addActionsAndClear();
+ void addActionsConnect();
void keyboardNavigation_data();
void keyboardNavigation();
@@ -264,6 +265,34 @@ void tst_QMenu::addActionsAndClear()
QCOMPARE(menus[0]->actions().count(), 0);
}
+static void testFunction() { }
+
+void tst_QMenu::addActionsConnect()
+{
+ QMenu menu;
+ const QString text = QLatin1String("bla");
+ const QIcon icon;
+ menu.addAction(text, &menu, SLOT(deleteLater()));
+ menu.addAction(text, &menu, &QMenu::deleteLater);
+ menu.addAction(text, testFunction);
+ menu.addAction(text, &menu, testFunction);
+ menu.addAction(icon, text, &menu, SLOT(deleteLater()));
+ menu.addAction(icon, text, &menu, &QMenu::deleteLater);
+ menu.addAction(icon, text, testFunction);
+ menu.addAction(icon, text, &menu, testFunction);
+#ifndef QT_NO_SHORTCUT
+ const QKeySequence keySequence(Qt::CTRL + Qt::Key_C);
+ menu.addAction(text, &menu, SLOT(deleteLater()), keySequence);
+ menu.addAction(text, &menu, &QMenu::deleteLater, keySequence);
+ menu.addAction(text, testFunction, keySequence);
+ menu.addAction(text, &menu, testFunction, keySequence);
+ menu.addAction(icon, text, &menu, SLOT(deleteLater()), keySequence);
+ menu.addAction(icon, text, &menu, &QMenu::deleteLater, keySequence);
+ menu.addAction(icon, text, testFunction, keySequence);
+ menu.addAction(icon, text, &menu, testFunction, keySequence);
+#endif // !QT_NO_SHORTCUT
+}
+
// We have a separate mouseActivation test for Windows mobile
#ifndef Q_OS_WINCE
void tst_QMenu::mouseActivation()
diff --git a/tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp b/tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp
index 24de35ce89..642abc3add 100644
--- a/tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp
+++ b/tests/auto/widgets/widgets/qtoolbar/tst_qtoolbar.cpp
@@ -67,6 +67,7 @@ private slots:
void allowedAreas();
void orientation();
void addAction();
+ void addActionConnect();
void insertAction();
void addSeparator();
void insertSeparator();
@@ -361,6 +362,23 @@ void tst_QToolBar::addAction()
}
}
+static void testFunction() { }
+
+void tst_QToolBar::addActionConnect()
+{
+ QToolBar tb;
+ const QString text = QLatin1String("bla");
+ const QIcon icon;
+ tb.addAction(text, &tb, SLOT(deleteLater()));
+ tb.addAction(text, &tb, &QMenu::deleteLater);
+ tb.addAction(text, testFunction);
+ tb.addAction(text, &tb, testFunction);
+ tb.addAction(icon, text, &tb, SLOT(deleteLater()));
+ tb.addAction(icon, text, &tb, &QMenu::deleteLater);
+ tb.addAction(icon, text, testFunction);
+ tb.addAction(icon, text, &tb, testFunction);
+}
+
void tst_QToolBar::insertAction()
{
QToolBar tb;