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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Digital God
  • 471
  • 1
  • 5
  • 17
  • If you have fixed your problem you should post it as an answer and accept it. That way it will benefit others more – musefan Nov 24 '11 at 14:13

1 Answers1

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!

TonyK
  • 16,761
  • 4
  • 37
  • 72
  • +1 for mentioning the diagnostic error messages. Those are really useful. I think a lot of people would neglect to read them. – Chris Parton Nov 24 '11 at 09:00
  • 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)` – Digital God Nov 24 '11 at 09:06
  • It looks like you're calling `connect` from a member function declared `const` (look at `Core* const` in the error message). Is that right? – TonyK Nov 24 '11 at 09:45