Qt主窗口菜单信号
我有一个处理QMainWindow的“核心”对象。Qt主窗口菜单信号,qt,connect,signals-slots,slot,Qt,Connect,Signals Slots,Slot,我有一个处理QMainWindow的“核心”对象。 Core.h代码 class Core : public QObject { Q_OBJECT public: explicit Core(QObject *parent = 0); ~Core(); void appInit(); int getAuth(); public slots: void appExit(); private slots: void appMenuTrigg
Core.h代码
class Core : public QObject
{
Q_OBJECT
public:
explicit Core(QObject *parent = 0);
~Core();
void appInit();
int getAuth();
public slots:
void appExit();
private slots:
void appMenuTriggered(QAction *action);
private:
void preInit();
MainWindow *mwnd;
};
Core.cpp代码
Core::Core(QObject *parent) : QObject(parent)
{
qDebug() << "Core::Constructor called";
preInit();
}
Core::~Core()
{
delete mwnd;
qDebug() << "Core::Destructor called";
}
int Core::getAuth()
{
LoginDialog *login = new LoginDialog();
int r = login->exec();
delete login;
return r;
}
void Core::appExit() // connected to qapplication aboutToQuit
{
qDebug() << "Core::appExit called";
}
void Core::preInit() // called after getAuth im main.cpp
{
qDebug() << "Core::preInit called";
}
void Core::appMenuTriggered( QAction *action )
{
qDebug() << "action triggered";
}
void Core::appInit()
{
mwnd = new MainWindow();
mwnd->show();
qDebug() << "Core::appInit called";
}
但它不起作用。我对C++和QT有新的认识。如何连接这个?
或者也许有更好的方法来处理其他程序部件的主窗口操作
UPD
问题解决了。忘记包括QMenuBar您必须在信号和插槽参数中提供完整的函数规范(但不包括参数名称)。像这样:
connect(mwnd->menuBar(),
SIGNAL(triggered(QAction*)),
this,
SLOT(appMenuTriggered(QAction*)));
如果在Qt Creator中调试此类代码,当未找到信号或插槽时,
connect
函数将向应用程序输出窗格写入诊断错误消息。我建议您在修复问题之前找到这些错误消息,以便您知道将来在哪里查找。信号和插槽很容易出错 必须在信号和插槽参数中提供完整的函数规范(但不包括参数名称)。像这样:
connect(mwnd->menuBar(),
SIGNAL(triggered(QAction*)),
this,
SLOT(appMenuTriggered(QAction*)));
如果在Qt Creator中调试此类代码,当未找到信号或插槽时,
connect
函数将向应用程序输出窗格写入诊断错误消息。我建议您在修复问题之前找到这些错误消息,以便您知道将来在哪里查找。信号和插槽很容易出错 +1用于提及诊断错误消息。这些真的很有用。我想很多人都会忽略阅读它们。我在QT中遇到以下错误:没有匹配的函数来调用“Core::connect(QMenuBar*,const char*,Core*const,const char*)”候选函数是:static bool QObject::connect(const QObject*,const char*,const QObject*,const char*,QT::ConnectionType)
看起来您是从声明了const
的成员函数调用connect
(查看错误消息中的Core*const
)。是吗?+1用于提及诊断错误消息。这些真的很有用。我想很多人都会忽略阅读它们。我在QT中遇到以下错误:没有匹配的函数来调用“Core::connect(QMenuBar*,const char*,Core*const,const char*)”候选函数是:static bool QObject::connect(const QObject*,const char*,const QObject*,const char*,QT::ConnectionType)
看起来您是从声明了const
的成员函数调用connect
(查看错误消息中的Core*const
)。是吗?如果你已经解决了你的问题,你应该把它作为一个答案发布并接受它。如果你已经解决了你的问题,你应该把它作为一个答案发布并接受它。这样,它将使其他人受益更多