如何在Qt应用中退出应用

在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,那么在QML中调用上面的方法是不够的,我们需要做添加如下的代码:

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
                                 https://github.com/liu-xiao-guo/quit2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值