qt 访问共享文件夹_qt iOS app 如何获取共享文件夹文件,如何使用document文档目录文件...

在QT平台上,访问iOS应用的共享文件夹和Document目录与其它平台不同。由于iOS应用通常只能访问沙箱内的文件,首先尝试通过`QDir::currentPath()`获取当前路径,但结果为根目录“/”。然后尝试使用`QCoreApplication::applicationDirPath()`,返回的是应用的Bundle路径。为了启用iTunes文件共享,需在.pro文件中添加配置,并在info.plist中设置“UIFileSharingEnabled”为true。然而,这些方法仍无法解决问题。最终,通过混合使用Objective-C和QT C++,实现了获取Documents路径的方法,路径为:“/var/mobile/Containers/Data/Application/BB26B5E6-3340-4601-A81B-DE5362B1C938/Documents”。
摘要由CSDN通过智能技术生成

how to get sharing document directory is something different with other qt platforms. IOS app always use files only in app's sandbox. So we just need to find it's sandbox directory. we may think following would work: QString docPath;

QDir dir;

docPath=dir.currentPath();

however in qt 5.5 IOS, it return only root directory "/", then we use another way: docPath=QCoreApplication::applicationDirPath(); result is "/private/var/mobile/Containers/Bundle/Application/D31978DF-1A05-4AB9-9C36-18EA2B9E5294/testXXX.app", is that the real sandbox directory? The answer is still no, because we can not show any files sync-in iPhone to testXXX app by iTunes. (in qt .pro file,

we add lines QMAKE_INFO_PLIST += aaa.plist in aaa.plist we add "UIFileSharingEnabled true" to enable file sharing with iTunes.) We have no patience to find other qt traditional way to solve this problem. We use mix IOS obj-c and qt c++ as follows: QString iOSObj::getDocDir(void) {

iOSObj::g_iOSObj = this;

//get Documents path NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *path=[paths objectAtIndex:0]; NSLog(@"path:%@",path);

QString temp = QString::fromNSString(path);

return temp; } Finally, the result is “/var/mobile/Containers/Data/Application/BB26B5E6-3340-4601-A81B-DE5362B1C938/Documents”

Qt访问共享文件夹需要使用QFile和QDir类,并且需要提供正确的共享文件夹路径和凭据(用户名和密码)。 以下是一个示例代码,可以访问共享文件夹并列出其中的文件文件夹: ``` c++ #include <QCoreApplication> #include <QFile> #include <QDir> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 共享文件夹路径 QString sharePath = "\\\\192.168.1.100\\share"; // 设置凭据 QFile::setPermissions(sharePath, QFile::ReadOwner | QFile::WriteOwner); QFile::setPermissions(sharePath, QFile::ReadUser | QFile::WriteUser); QFile::setPermissions(sharePath, QFile::ReadGroup | QFile::WriteGroup); QFile::setPermissions(sharePath, QFile::ReadOther | QFile::WriteOther); QFile file(sharePath); file.setPermissions(QFile::ReadOwner | QFile::WriteOwner); file.setPermissions(QFile::ReadUser | QFile::WriteUser); file.setPermissions(QFile::ReadGroup | QFile::WriteGroup); file.setPermissions(QFile::ReadOther | QFile::WriteOther); // 设置用户名和密码 file.setUserName("username"); file.setPassword("password"); // 打开共享文件夹 QDir dir(sharePath); if (!dir.exists()) { qDebug() << "共享文件夹不存在。"; return a.exec(); } dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); QFileInfoList fileList = dir.entryInfoList(); foreach (QFileInfo fileInfo, fileList) { qDebug() << fileInfo.fileName(); } return a.exec(); } ``` 在上面的示例代码中,我们使用`QFile::setPermissions()`和`QFile::setUserName()`和`QFile::setPassword()`函数设置共享文件夹的凭据(用户名和密码)。然后,我们使用`QDir`类打开共享文件夹,并使用`QDir::entryInfoList()`函数列出其中的文件文件夹。 请注意,此示例代码中的用户名和密码是硬编码的,这是不安全的。在实际应用中,应该将用户名和密码存储在安全的地方,并动态地获取它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值