tags:

views:

133

answers:

3

Hi all

i'm trying to make a simple GUI with QT 4.6. i made a separete class that represents the menu bar:

MenuBar::MenuBar()
{
    aboutAct = new QAction(tr("&About QT"), this);
    aboutAct->setStatusTip(tr("Show the application's About box"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

    quitAct = new QAction(tr("&Quit"),this);
    quitAct->setStatusTip(tr("Exit to the program"));
    //connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));

    menuFile = new QMenu("File");
    menuFile->addAction(quitAct);

    menuLinks = new QMenu("Links");

    menuAbout = new QMenu("Info");
    menuAbout->addAction(aboutAct);


    addMenu(menuFile);
    addMenu(menuLinks);
    addMenu(menuAbout);
}

i can't connect the signal of the quitAct with the quit slot of the main application probably because it is not visible from the MenuBar class..

//connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));

how can i do it?

A: 

Hi.

  1. You may make your quitAct variable as public in MenuBar class, and then from the main window class connect menuBar->quitAct to you quit() function, like

    connect(menuBar->quitAct, SIGNAL(clicked()), this, SLOT(quit()));

  2. You may create public SIGNAL in MenuBar class, for example quitSignal(), and from the main window/dialog class connect it to the quit() function when this signal emits.

  3. You may not use MenuBar class code fo\rom outside of your main window/dialog class, and just put it all in one function, for example:

    void createMenuBar() { ... }

Good luck.

mosg
All methods are so "dirty" and not Qt style. And you didn't spot error in his code
Kamil Klimek
+4  A: 

Use the static instance() method of the QApplication class or the qApp macro to get the pointer to the application instance. Note that this is a pointer, so pass qApp to connect(), not &qApp.

balpha
+2  A: 

You have a typo. :)

In: connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));

The variable's name is qApp, not QApp. That aside, balpha said it all. So it's either:

connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
or
connect(quitAct, SIGNAL(triggered()), QApplication::instance(), SLOT(quit()));

Robin