Android和IOS上的Qt会话管理 [英] Qt session management on Android and IOS

查看:37
本文介绍了Android和IOS上的Qt会话管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Qt Quick Controls 2开发适用于Android和IOS的应用程序,并且可以登录.帐户凭据将存储到外部SQL数据库中.我已经弄清楚了如何与数据库进行交互,但是我无法弄清楚如何在Qt for Android和IOS中使用会话,从而使用户不必每次都键入登录凭据.

I am currently developing an app for Android and IOS using Qt Quick Controls 2 which will have login possibility. The account credentials will be stored into an external SQL database. I have figured out how I can interact with the database, but I am not able to figure out how I can use sessions in Qt for Android and IOS such that the user do not need to type the login credentials each time.

一种可能的方法是将凭据存储到手机上的本地文件中,然后在每次启动应用程序时访问该文件.但是,我不确定这是否是最好和最安全的方法.

One possible way could be to store the credentials to a file locally on the phone, and then access the file each time the app is started. However, I am not sure if this is the best and safest way.

谢谢.

推荐答案

QSettings 类用于状态管理.QSettings信息通常存储在Windows上的系统注册表中,以及macOS和iOS上的属性列表文件中.这是使用QSettings存储/检索会话参数的原始示例代码.您可以扩展以包括所需的会话登录管理方案:

QSettings class is used for state management. QSettings information is often stored in the system registry on Windows, and in property list files on macOS and iOS. Here is raw example code that uses QSettings to store/retrieve session parameters .. you can extend to include needed session login management scenarios:

main.cpp

int main( int argc, char ** argv ) {
QApplication app( argc, argv );
app.setOrganizationName("myOrg");
app.setOrganizationDomain("myOrg.net");
app.setApplicationName("qsettings-test");
MyMainWindow mw;
mw.show();
return app.exec();
}

然后在您的源文件.cpp中

Then in your source .cpp

void MyMainWindow::readSettings() {
QSettings settings;
QString user = settings.value("UserName");
int lifTime = settings.value("sessionLifeTime").toInt();;
QByteArray state = settings.value("state", QByteArray())
                                               .toByteArray();
restoreState(state);
}

// ...... lots of your code 

void MyMainWindow::closeEvent(QCloseEvent* event) {
if (maybeSave()) {
    writeSettings();
    event->accept();
} else {
    event->ignore();
}
}


void MyMainWindow::writeSettings() {
    /* Save postion/size of main window */
    QSettings settings;
    settings.setValue("sessionLifeTime", 7);
    settings.setValue("UserName", user);
    settings.setValue("state", saveState());
}

您还可以使用 QSettings 类访问INI和PLIST配置文件,对于Android,可以考虑在构建过程中必须在其中复制INI文件的位置(存储在res文件夹中,并将其包含在.pro中)

You can also access INI and PLIST configuration files with QSettings class, For Android this can be considered where your INI file must be copied during build (store in res folder and include it in .pro ).

这篇关于Android和IOS上的Qt会话管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆