Qt Mainwindow menu signals
Asked Answered
C

1

1

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

Communalism answered 24/11, 2011 at 8:15 Comment(1)
If you have fixed your problem you should post it as an answer and accept it. That way it will benefit others moreSoutor
E
7

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!

Exarate answered 24/11, 2011 at 8:31 Comment(3)
+1 for mentioning the diagnostic error messages. Those are really useful. I think a lot of people would neglect to read them.Thimble
I get following error in QT: no matching function for call to 'Core::connect(QMenuBar*, const char*, Core* const, const char*)' candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)Communalism
It looks like you're calling connect from a member function declared const (look at Core* const in the error message). Is that right?Exarate

© 2022 - 2024 — McMap. All rights reserved.