gpt4 book ai didi

c++ - 检查应用程序是否首次运行

转载 作者:行者123 更新时间:2023-11-30 04:49:19 26 4
gpt4 key购买 nike

我是 qt 移动开发的新手,我有一个相当愚蠢的问题。我如何检查用户是否是第一次运行该应用程序(Android 和 iOS)?

编辑:

我需要这个检查的原因是我有一个介绍 SwipeView 的初学者,一旦阅读它应该总是打开主应用程序屏幕。

我已经尝试了@TrebledJ 建议的方式,它似乎工作正常,或者在 main.cpp 中这样做是不是很愚蠢?

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSettings>

int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

QGuiApplication app(argc, argv);

QSettings settings;
QVariant firstRun = settings.value("first-run");

QQmlApplicationEngine engine;
QUrl startingScreen(QStringLiteral("qrc:/main.qml"));

if(!firstRun.isValid())
settings.setValue("first-run", true);
else
startingScreen.setUrl(QStringLiteral("qrc:/start.qml"));

engine.load(startingScreen);
if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}

最佳答案

使用QSettings检查设定值。

QSettings settings;
QVariant val = settings.value("first-time");
if (!val.isValid()) {
// ... first run
settings.setValue("first-time", false); // set a value so that the value is valid on the next run
} else {
// ... not first run
}

在 QML 中,有 Settings QML 类型。

import Qt.labs.settings 1.0

Settings {
id: settings
property bool isFirstTime: true
}

Component.onCompleted: {
if (settings.isFirstTime) {
// ... first run
settings.isFirstTime = false;
} else {
// ... not first run
}
}

但是,根据文档:

Note: This type is made available by importing the Qt.labs.settings module. Types in the Qt.labs module are not guaranteed to remain compatible in future versions.

考虑到非保证,Felgo/V-Play的API有一个Storage QML 类型,它也可以在 QML 中执行检查。 (他们文档中的第一个示例实现了这一点。)

关于c++ - 检查应用程序是否首次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55458179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com