Closing a Qt Quick application from QML

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Overview[edit | edit source]

This snippet shows how to trigger the closing of a Qt Quick application from QML code. This is needed especially in full screen applications, because there may not be any dedicated hardware key to close the application.

The following QML code calls the global 76ytuiytuityutyutututyutyutyu function which will trigger the ending of the application. In this example, we have used 76ytuiytuityutyutututyutyutyu to show the Qt Quick UI and thus we must connect the 76ytuiytuityutyutututyutyutyu signal to the 76ytuiytuityutyutututyutyutyu slot.

NOTE: Had we used 76ytuiytuityutyutututyutyutyu to interpret the QML, the 76ytuiytuityutyutututyutyutyu signal would have been automatically handled.

Preconditions[edit | edit source]

  • Qt 4.7 or higher is installed on your platform.

Qt Project File[edit | edit source]

  1. To make sure we use declarative

QT += declarative

  1. To get files deployed on device / emulator

files.sources += ui.qml DEPLOYMENT += files

Source[edit | edit source]

main.cpp

  1. include <QApplication>
  2. include <QDeclarativeView>
  3. include <QDeclarativeEngine>

int main(int argc, char *argv[]) {

   QApplication app(argc, argv);
   QDeclarativeView view;
   view.setSource(QUrl("./ui.qml"));
   view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
   QObject::connect((QObject*)view.engine(), SIGNAL(quit()), &app, SLOT(quit()));
  1. if defined(Q_WS_S60) || defined(Q_WS_MAEMO)
   view.showMaximized();
  1. else
   view.setGeometry(100,100, 800, 480);
   view.show();
  1. endif
   return app.exec();

}

ui.qml

import Qt 4.7

Rectangle {

   anchors.fill: parent; color: "black"
   Rectangle {
       anchors.centerIn: parent
       width: 100; height: 40; radius: 5; color: "lightgray"
       Text { anchors.centerIn: parent; text: "Quit"; color: "black" }
       MouseArea {
           anchors.fill: parent
           onClicked: Qt.quit()
       }
   }

}

Postconditions[edit | edit source]

The calling of the 76ytuiytuityutyutututyutyutyu function in QML code caused the emission of the 76ytuiytuityutyutututyutyutyu signal, and because this signal was connected to the 76ytuiytuityutyutututyutyutyu slot, the application was closed.