Qt Mainwindow菜单信号 [英] Qt Mainwindow menu signals

查看:108
本文介绍了Qt Mainwindow菜单信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理QMainWindow的"Core"对象.
Core.h代码

I've "Core" object that handles QMainWindow.
Core.h code

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.cpp code

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";
}

我正在尝试将mainwindow菜单栏信号连接到核心插槽,如下所示:

I'm trying to connect mainwindow menubar signal to core slot like this:

connect(mwnd->menuBar(), SIGNAL(triggered()), this, SLOT(appMenuTriggered()));

但是它不起作用.我是C ++和Qt的新手.如何连接呢?或者也许有更好的方法来处理其他程序部件的主窗口操作.

But it doesn't work. Im new to c++ and Qt. How to connect this? Or maybe there is better way to handle mainwindow actions to other programm parts.

UPD 问题解决了.忘记包含QMenuBar

UPD Problem solved. Forget to include QMenuBar

推荐答案

您必须在SIGNAL和SLOT参数中提供完整的功能规范(但不带参数名称).像这样:

You have to give the full function spec in the SIGNAL and SLOT parameters (but without the argument names). Like this:

connect(mwnd->menuBar(),
        SIGNAL(triggered(QAction*)),
        this,
        SLOT(appMenuTriggered(QAction*)));

如果您在Qt Creator中调试此类代码,则 connect 函数将在找不到信号或插槽时将诊断错误消息写入应用程序输出"窗格.我建议您在解决问题之前先找到这些错误消息,以便将来知道要去哪里.很容易弄错信号和插槽!

If you debug such code in Qt Creator, the connect function will write diagnostic error messages to the Application Output pane when it doesn't find a signal or a slot. I suggest that you find these error messages before you fix your problem, so that you know where to look in future. It's very easy to get signals and slots wrong!

这篇关于Qt Mainwindow菜单信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆