Ryseth
Ryseth

Reputation: 79

QT- Menu's Exit option

I'm finally about to finish my school project and thought it'd be nice to have an exit option in my drop-down menu, however, no matter what I try I can't make it work. If you could have a look at it in your spare time, I'd really appreciate it (or any suggestions you might have)

void MainWindow::on_actionExit_triggered()
{
    connect(this,SIGNAL(triggered()),MainWindow,SLOT(close()));
} 

Every time I try to debug it, it gives the following error message:

error: expected primary-expression before ',' token

Upvotes: 3

Views: 8694

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

  1. You need to pass object pointer to connect function, not class name. You should replace MainWindow to this. It will fix the compilation error.

  2. It would be wiser to put connect statement in the class constructor. You need to do the connect at initialization if you want it to work any time the user presses the menu item.

  3. on_actionExit_triggered has special name form. Once you create a slot with such a name, Qt will automatically connect it to the triggered slot of the actionExit action (see Automatic Connections for more information.). So there is no need for connect statement at all.

  4. The on_actionExit_triggered slot will be called when the user presses the menu item. Of course you need to put some implementation in it. For example:

    void MainWindow::on_actionExit_triggered() {
        QApplication::quit();
    }
    

Upvotes: 13

Related Questions