1

I have a QML app in which I have subclassed QApplication to create my main screen with QML. The issue i have is on clicking Close button the application closes as intended, but I want to handle a situation where if some services are running I want to override close button behaviour.

I tried overriding closeEvent() without any luck. Can anyone point me to some ways I can handle this?

UPDATE : This is the code snippet I tried

class SingleApplication : public QApplication {
    Q_OBJECT
public:
    SingleApplication(int &argc, char **argv);

    void closeEvent ( QCloseEvent * event )
    {
        event->ignore();

    }
}

MAIN.cpp

#include "view.h"
#include <QDebug>
#include <QDesktopWidget>
#include "SingleApplication.h"

int main(int argc, char *argv[])
{
    SingleApplication app(argc, argv);
    if(!app.isRunning()) {

        app.processEvents();

        View view(QUrl("qrc:/qml/main.qml"));
#ifdef Q_OS_LINUX
        view.setFlags(Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint);
#endif
        view.setMaximumSize(QSize(1280,700));
        view.setMinimumSize(QSize(1280,700));

        // Centering the App to the middle of the screen
        int width = view.frameGeometry().width();
        int height = view.frameGeometry().height();
        QDesktopWidget wid;
        int screenWidth = wid.screen()->width();
        int screenHeight = wid.screen()->height();
        view.setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height);


        view.show();

        return app.exec();
    }
    return 0;

}
prakashpun
  • 259
  • 4
  • 19
  • 1
    Some corresponding code can help you to get right answer. What did you already try? What was an error? – folibis Jul 10 '15 at 08:22
  • Try to use [`QApplication::setQuitOnLastWindowClosed()`](http://doc.qt.io/qt-4.8/qapplication.html#quitOnLastWindowClosed-prop) – t3ft3l--i Jul 10 '15 at 08:25
  • You didn't paste code that has a function called `closingDown()`. Please post a [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/). – Mitch Jul 10 '15 at 18:20

1 Answers1

2

There is no QApplication::closeEvent. Such virtual function belongs to QWidget.

Use of QApplication indicated that you have normal QWidget container for your QML UI (as you say UI is based on QML though). You should rather override that widget closeEvent e.g.:

class MyMainWidget : public QWidget // or is it QMainWindow?
{
   // snip
private:
    void closeEvent(QCloseEvent*);
}

void MyMainWidget::closeEvent(QCloseEvent* event)
{
    // decide whether or not the event accepted
    if (condition())
       event->accept();
}

And if your container widget is not overridden yet (simply QWidget?), well, now you have to do so.

And you did not say whether or not you want to keep app window running. I assume you want that as well.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • HI @AlexanderVX , So you mean to say I need to change QApplication to QWidget ? Sorry if this is a dumb question, I have no clue about this! – prakashpun Jul 13 '15 at 10:14
  • Can you provide main.cpp code? That will likely show. I mean you very likely use QWidget or its child there. – Alexander V Jul 13 '15 at 18:54
  • Hi @AlexanderVX. I have attached my main.cpp code. NOte that SingleApplication is the class that subclasses QApplication. – prakashpun Jul 14 '15 at 04:34
  • Where is the class View? It must be derived from QWidget and acts as a container for QML View. Other than that you won't be able to use it like that. And that class should be added with closeEvent() handler – Alexander V Jul 14 '15 at 04:46