我使用的是Qt 5.7,我有一个行为类似于QMenuBar的组件。此组件有两个方法,一个名为addMenu(QPushButton* menu)
,用于添加条形图菜单,另一个名为addMenuItem(QPushButton* menu, QString item, const char *slot)
,用于添加构成其中一个条形菜单的项目。在第二种方法中,我调用QMenu的addAction方法,并且必须向它传递两个参数:项目名称和触发项目时将运行的插槽。我的问题是如何做到这一点。
我做了一个搜索(参见here),我发现QT将插槽视为const char *
,但是当我尝试使用此方法签名编译我的程序时,它会给出错误,说明没有操作符( )注册此类型。
我的代码:
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;
答案 0 :(得分:4)
以下是您尝试使用的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.