From 79d3351855ea055bed0b5b752a4a6fed51a27f9b Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Tue, 2 Jan 2018 16:57:39 +0300 Subject: QMenuBar: Add overloads of addAction() using Qt 5 signals and slots [ChangeLog][QtWidgets][QMenuBar] Add overloads of addAction() using Qt 5 signals and slots Change-Id: Ief21974213b80111f0ca87df490eb72dd6b9c9b9 Reviewed-by: Martin Smith --- src/widgets/widgets/qmenubar.cpp | 36 ++++++++++++++++++++ src/widgets/widgets/qmenubar.h | 26 +++++++++++++++ .../auto/widgets/widgets/qmenubar/tst_qmenubar.cpp | 38 ++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/src/widgets/widgets/qmenubar.cpp b/src/widgets/widgets/qmenubar.cpp index 80c6d28e85..384c3b4c2c 100644 --- a/src/widgets/widgets/qmenubar.cpp +++ b/src/widgets/widgets/qmenubar.cpp @@ -780,6 +780,42 @@ QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const return ret; } +/*! + \fn template + QAction *QMenuBar::addAction(const QString &text, const Obj *receiver, PointerToMemberFunctionOrFunctor method) + + \since 5.11 + + \overload + + This convenience function creates a new action with the given \a + text. The action's 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. + + QMenuBar takes ownership of the returned QAction. + + \sa QWidget::addAction(), QWidget::actions() +*/ + +/*! + \fn template + QAction *QMenuBar::addAction(const QString &text, Functor functor) + + \since 5.11 + + \overload + + This convenience function creates a new action with the given \a + text. The action's 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. + + QMenuBar takes ownership of the returned QAction. + + \sa QWidget::addAction(), QWidget::actions() +*/ + /*! Appends a new QMenu with \a title to the menu bar. The menu bar takes ownership of the menu. Returns the new menu. diff --git a/src/widgets/widgets/qmenubar.h b/src/widgets/widgets/qmenubar.h index 4f8e3b8d53..2f071e7e3b 100644 --- a/src/widgets/widgets/qmenubar.h +++ b/src/widgets/widgets/qmenubar.h @@ -67,6 +67,32 @@ public: QAction *addAction(const QString &text); QAction *addAction(const QString &text, const QObject *receiver, const char* member); +#ifdef Q_QDOC + template + QAction *addAction(const QString &text, const Obj *receiver, PointerToMemberFunctionOrFunctor method); + template + QAction *addAction(const QString &text, Functor functor); +#else + // addAction(QString): Connect to a QObject slot / functor or function pointer (with context) + template + inline typename std::enable_if::value + && QtPrivate::IsPointerToTypeDerivedFromQObject::Value, QAction *>::type + addAction(const QString &text, const Obj *object, Func1 slot) + { + QAction *result = addAction(text); + connect(result, &QAction::triggered, object, std::move(slot)); + return result; + } + // addAction(QString): Connect to a functor or function pointer (without context) + template + inline QAction *addAction(const QString &text, Func1 slot) + { + QAction *result = addAction(text); + connect(result, &QAction::triggered, std::move(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/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp index 96a64b18c1..26ef228130 100644 --- a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp +++ b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp @@ -153,6 +153,8 @@ private slots: void platformMenu(); + void addActionQt5connect(); + protected slots: void onSimpleActivated( QAction*); void onComplexActionTriggered(); @@ -1595,6 +1597,42 @@ void tst_QMenuBar::platformMenu() QVERIFY(menu->platformMenu()); } +class TestObject : public QObject +{ + Q_OBJECT +public: + bool flag = false; + void setFlag() + { + flag = true; + } +}; + +void tst_QMenuBar::addActionQt5connect() +{ + bool flag = false; + auto functor = [&flag](){ flag = true; }; + + TestObject obj; + + QMenuBar menuBar; + + auto action1 = menuBar.addAction(QStringLiteral("1"), &obj, &TestObject::setFlag); + auto action2 = menuBar.addAction(QStringLiteral("2"), functor); + + action1->activate(QAction::Trigger); + action2->activate(QAction::Trigger); + + QVERIFY(obj.flag); + QVERIFY(flag); + + flag = false; + + auto action3 = menuBar.addAction(QStringLiteral("3"), this, functor); + action3->activate(QAction::Trigger); + QVERIFY(flag); +} + void tst_QMenuBar::slotForTaskQTBUG53205() { QWidget *parent = taskQTBUG53205MenuBar->parentWidget(); -- cgit v1.2.3