Reputation: 752
I have a QML window declared for example in MyWindow.qml:
Item {
id: thisWindow
width: 500
height: 140
... sub-items that declare the UI of the window ...
And a C++ class that instantiates that QML:
class MyWindow : public QQuickView
...
MyWindow::MyWindow() {
setSource(QUrl("qrc:/MyWindow.qml"));
setFlags(Qt::WindowFlags(Qt::Popup));
}
How do I close that window from Javascript/QML code? I can't call thisWindow.close(), because it's just an item type in the hierarchy.
Upvotes: 2
Views: 1990
Reputation: 7170
You don't need c++ to do that. You can do it with the window
attached property straight from QML.
//other imports
import QtQuick.Window 2.2
Item {
id: thisWindow
width: 500
height: 140
//... sub-items that declare the UI of the window ...
MouseArea {
anchors.fill: parent
onClicked: Window.window.close()
}
}
Upvotes: 5
Reputation: 565
Use the Qt global object and follow the instruction here: http://doc.qt.io/qt-5/qml-qtqml-qt.html#quit-method
Upvotes: 1
Reputation: 244291
The easiest option is to export the QQuickView
to the .qml with setContextProperty()
:
#include <QQmlEngine>
#include <QQmlContext>
// ...
{
engine()->rootContext()->setContextProperty("view", this);
setSource(QUrl("qrc:/MyWindow.qml"));
setFlags(Qt::WindowFlags(Qt::Popup));
}
And then in QML you can use:
view.close()
Upvotes: 3