Ricardo Melo Ricardo Melo - 3 months ago 46
C++ Question

How to pass a slot as an argument

I'm using Qt 5.7 and a I have a component that behaves like a QMenuBar. This component has two methods, one, called

addMenu(QPushButton* menu)
, to add a bar menu and another, called
addMenuItem(QPushButton* menu, QString item, const char *slot)
to add the items that make up one of the bar menus. In this second method, I call addAction method of QMenu, and must pass two arguments to it: the item name and the slot that will run when the item is triggered. My question is how to do it.
I did a search (see here) and I found that QT treat slots as
const char *
, but when I try to compile my program using this method signature, it gives error, stating that there is no operator() registered for this type.

My code:

GMInternalMenu::addMenuItem(QPushButton *menu, QString menuItem, const char* slot){
QMenu *dropdown = mapa->value(menu);
dropdown->addAction(menuItem, slot);
}


Using the method:

ui->imMenu->addMenuItem(someButton, "Some action", SLOT(close()));


Output error:
(Also on pastebin)

In file included from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtGui/qwindowdefs.h:43:0,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qwidget.h:43,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qmenu.h:43,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/QMenu:1,
from ../Project/mymenu.h:4,
from ../Project/mymenu.cpp:1:
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobject.h: In instantiation of 'static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) [with Func1 = void (QAction::*)(bool); Func2 = const char*; typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = QAction]':
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobject.h:293:74: required from 'static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (QAction::*)(bool); Func2 = const char*; typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = QAction]'
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qmenu.h:120:9: required from 'QAction* QMenu::addAction(const QString&, Func1, const QKeySequence&) [with Func1 = const char*]'
../Project/mymenu.cpp:35:39: required from here
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qglobal.h:746:47: error: static assertion failed: Signal and slot arguments are not compatible.
#define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
^
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobject.h:306:9: note: in expansion of macro 'Q_STATIC_ASSERT_X'
Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0),
^
In file included from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobjectdefs.h:50:0,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtGui/qwindowdefs.h:44,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qwidget.h:43,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qmenu.h:43,
from C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/QMenu:1,
from ../Project/mymenu.h:4,
from ../Project/mymenu.cpp:1:
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobjectdefs_impl.h: In instantiation of 'struct QtPrivate::FunctorReturnType<const char*, QtPrivate::List<> >':
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobject.h:309:158: required from 'static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) [with Func1 = void (QAction::*)(bool); Func2 = const char*; typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = QAction]'
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobject.h:293:74: required from 'static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (QAction::*)(bool); Func2 = const char*; typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = QAction]'
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtWidgets/qmenu.h:120:9: required from 'QAction* QMenu::addAction(const QString&, Func1, const QKeySequence&) [with Func1 = const char*]'
../Project/mymenu.cpp:35:39: required from here
C:/Qt/Qt5.7.0/5.7/mingw53_32/include/QtCore/qobjectdefs_impl.h:632:78: error: request for member 'operator()' in 'QtPrivate::FunctorReturnType<Functor, QtPrivate::List<Tail ...> >::dummy<const char*>()', which is of non-class type 'const char*'
typedef decltype(dummy<Functor>().operator()((dummy<ArgList>())...)) Value;

Answer

Here is the declaration of the QMenu::addAction method you are trying to use:

QAction * addAction(const QString &text, 
                    const QObject *receiver, 
                    const char *member, 
                    const QKeySequence &shortcut = 0);

As you can see, you are missing the receiver parameter, which is the object that will receive the slot. From your code, I guess you should have something like:

GMInternalMenu::addMenuItem(QPushButton *menu, QString menuItem, 
                            QObject *receiver, const char* slot){
    QMenu *dropdown = mapa->value(menu);
    dropdown->addAction(menuItem, receiver, slot);
}

And then when you call it:

ui->imMenu->addMenuItem(someButton, "Some action", this, SLOT(close()));
//                                                 ^^^^
//                                                 Maybe something else... I don't know
//                                                 on which object you need the slot to be
//                                                 called.
Comments