在iOS设备上找不到文件QT创建者C QstandardPaths :: StandardLocations(Qs

No files found on iOS devices Qt Creator C++ QStandardPaths::standardLocations(QStandardPaths::DownloadLo‌​cation);

本文关键字:QstandardPaths StandardLocations Qs 创建者 QT iOS 文件 找不到      更新时间:2023-10-16

我正在使用QT创建者C QstandardPaths :: standardLocations(qstandardpaths ::下载lo‌ cation);要到达用户的文件,但它返回在iOS设备上找不到文件。到目前为止,这是我的代码:

const QStringList
mPathList = QStandardPaths::standardLocations(QStandardPaths::DownloadLo‌​cation);
mPath = QString("%1").arg(mPathList.first());
dirmodel = new QFileSystemModel (this);
dirmodel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
dirmodel->setRootPath(mPath); 

在iOS上,所有应用程序都生活在自己的沙箱中,您永远无法直接访问应用程序外的事物/文件。

要这样做,您需要在运行时调用您的应用程序中的本机iOS API。同样,最好下次启动应用程序时,不要保存路径来使用它们,因为iOS会生成Sandobox中应用程序的基于哈希的路径,并且下次您运行应用程序时可以更改。

这是如何在qt中调用本机iOS代码的示例。

创建具有以下内容的ios_utils.mm,并将其添加到您项目的源目录中:

#include "ios_utils.h"
#include <QStringList>
#include <UIKit/UIKit.h>
#include <qpa/qplatformnativeinterface.h>

QStringList getiOSHomeFolder()
{
    QStringList retval;
    NSString *item;
    NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSHomeDirectory() error:nil];
    for (item in contents)
    {
        retval.append(QString::fromNSString(item));
    }
    return retval;
}

创建ios_utils.h

#ifndef IOSUTILS_H
#define IOSUTILS_H

#ifdef Q_OS_IOS
QStringList getiOSHomeFolder();
#endif // Q_OS_IOS
#endif // IOSUTILS_H

以及QT代码中的某个地方:

#include "ios_utils.h"
qDebug() << getiOSHomeFolder();