Qt Mainwindow menu signals

孤街浪徒 提交于 2019-12-24 11:05:26

问题


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

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

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

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 Problem solved. Forget to include QMenuBar


回答1:


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*)));

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!



来源:https://stackoverflow.com/questions/8253918/qt-mainwindow-menu-signals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!