0

I am trying to use the QCloseEvent to close all my dialogs and quit the application when the main window closes. I have read the documentation and looked at many examples and this is what I've come up with so far:

In my .h file:

protected:

void mainwindow::closeEvent(QCloseEvent * );

In my mainwindow.cpp file:

class QCloseEvent;
void mainwindow::closeEvent(QCloseEvent *event)
{
    event->accept();

    if (event->isAccepted())
    {
        QApplication::quit();
    }

}

when I run this code I get the following errors: mainwindow::closeEvent' : local function definitions are illegal mainwindow.cpp(13): this line contains a '{' which has not yet been matched

theChef613
  • 127
  • 1
  • 9

1 Answers1

0

There is a problem with function declaration in your .h file :

void mainwindow::closeEvent(QCloseEvent * );

It should be :

void closeEvent(QCloseEvent * );

Also include QCloseEvent instead of class QCloseEvent;.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • I changed the header file as suggested and i still get the following error mainwindow.cpp:22: error: C2601: 'mainwindow::closeEvent' : local function definitions are illegal simmotioncontrol.cpp(13): this line contains a '{' which has not yet been matched... the errors are showing up in my .cpp file, not the .h file if that helps – theChef613 Jul 13 '14 at 19:26
  • Remove the forward declaration from the cpp, which is `class QCloseEvent;` and include `QCloseEvent` header into either your cpp or h file. – Murat Şeker Jul 13 '14 at 19:48
  • I did that as well and now I get the same error as before (local function definitions are illegal) thank you for your help void simMotionControl::closeEvent(QCloseEvent *event) { event->accept(); if (event->isAccepted()) { QApplication::quit(); } return; } – theChef613 Jul 13 '14 at 20:46
  • I found the problem. I was putting the code just after (inside the brakets) `ui->setupUi(this);` i just had to take the code outside those brakets – theChef613 Jul 13 '14 at 22:13