在Ubuntu平台上,我们一般不需要退出自己的应用.我们一般交给操作系统来帮我们关掉自己的应用(在资源紧张时,有系统的调度器去管理).当然,我们也可以在手机中,从最右边向左滑动,出现如下的应用管理器:
我们可以向上,或向下滑动就可以关掉该应用.
如果我们想在自己的应用中关掉我们的应用,我们应该怎么做呢?
在纯的QML中,我们可以直接调用如下的方法直接退出我们的应用:
Main.qml
import QtQuick 2.0
import Ubuntu.Components 1.1
/*!
\brief MainView with a Label and Button elements.
*/
MainView {
// objectName for functional testing purposes (autopilot-qt5)
objectName: "mainView"
// Note! applicationName needs to match the "name" field of the click manifest
applicationName: "quit1.liu-xiao-guo"
/*
This property enables the application to change orientation
when the device is rotated. The default is false.
*/
//automaticOrientation: true
// Removes the old toolbar and enables new features of the new header.
useDeprecatedToolbar: false
width: units.gu(60)
height: units.gu(85)
Page {
title: i18n.tr("quit1")
Column {
spacing: units.gu(1)
anchors {
margins: units.gu(2)
fill: parent
}
Button {
objectName: "button"
width: parent.width
text: i18n.tr("Exit")
onClicked: {
Qt.quit()
}
}
}
}
}
这里我们调用:
Qt.quit();
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///Main.qml")));
view.setResizeMode(QQuickView::SizeRootObjectToView);
QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit()));
view.show();
return app.exec();
}
QObject::connect(view.engine(), SIGNAL(quit()), qApp, SLOT(quit()));
整个项目的源码在:
https://github.com/liu-xiao-guo/quit1