0

What signal does it tell me when the QQuickWidget closes?

For example I wrote the following code :

QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("main.qml"));
view->show();

I have an ApplicationWindow in the main.qml file and I want to execute some code right after the qml window gets closed.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Parsa Mousavi
  • 1,052
  • 1
  • 13
  • 31

1 Answers1

0

The solution is to implement a Helper that is injected into QML and notifies us when the window is closed:

class Helper: public QObject{
    Q_OBJECT
public:
    using QObject::QObject;
Q_SIGNALS:
    void closed();
};
Helper *helper = new Helper;
QObject::connect(helper, &Helper::closed, [](){
   qDebug() << "closed";
});

QQuickWidget *view = new QQuickWidget;
view->rootContext()->setContextProperty("helper", helper);
view->setSource(QUrl::fromLocalFile("main.qml"));
view->show();
ApplicationWindow {
    // ...
    onClosing: helper.closed()
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Why do I get the error "C++ requires a type specifier for all declarations" for the "QObject::connect()" line ? I've included and also added Q_OBJECT macro as in your answer. – Parsa Mousavi May 18 '20 at 20:23