如何将插槽作为参数传递
Posted
技术标签:
【中文标题】如何将插槽作为参数传递【英文标题】:How to pass a slot as an argument 【发布时间】:2016-08-12 13:49:11 【问题描述】:我正在使用 Qt 5.7,并且我有一个行为类似于 QMenuBar 的组件。该组件有两种方法,一种称为addMenu(QPushButton* menu)
,用于添加条形菜单,另一种称为addMenuItem(QPushButton* menu, QString item, const char *slot)
,用于添加构成条形菜单之一的项目。在第二个方法中,我调用 QMenu 的 addAction 方法,并且必须向它传递两个参数:项目名称和触发项目时将运行的插槽。我的问题是怎么做。
我进行了搜索(请参阅here),我发现 QT 将插槽视为const char *
,但是当我尝试使用此方法签名编译我的程序时,它给出了错误,说明没有为此注册 operator()输入。
我的代码:
GMInternalMenu::addMenuItem(QPushButton *menu, QString menuItem, const char* slot)
QMenu *dropdown = mapa->value(menu);
dropdown->addAction(menuItem, slot);
使用方法:
ui->imMenu->addMenuItem(someButton, "Some action", SLOT(close()));
输出错误: (也在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;
【问题讨论】:
你使用的是Qt4还是Qt5? 另外,请将错误粘贴到您的问题中,有些人无法访问 pastebin.com ;) 【参考方案1】:这是您尝试使用的QMenu::addAction
方法的声明:
QAction* addAction(const QString &text,
const QObject *receiver,
const char *member,
const QKeySequence &shortcut = 0);
如您所见,您缺少receiver
参数,这是将接收信号的对象。
从你的代码中,我猜你应该有类似的东西:
GMInternalMenu::addMenuItem(QPushButton *menu, QString menuItem,
QObject *receiver, const char* slot)
QMenu *dropdown = mapa->value(menu);
dropdown->addAction(menuItem, receiver, slot);
然后当你使用它时:
ui->imMenu->addMenuItem(someButton, "Some action", this, SLOT(close()));
// ^^^^
// Maybe something else... I do not know
// which object should receive the signal.
【讨论】:
以上是关于如何将插槽作为参数传递的主要内容,如果未能解决你的问题,请参考以下文章