Qt - 访问束路径 [英] Qt - accessing the bundle path

查看:322
本文介绍了Qt - 访问束路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Qt文档 Mac差异页面提供了访问应用程序包路径的以下代码:

The Qt documentation "Mac Differences" page provides the following code for accessing an application's bundle path:

CFURLRef appUrlRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef macPath = CFURLCopyFileSystemPath(appUrlRef, kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath,CFStringGetSystemEncoding());
qDebug("Path = %s", pathPtr);
CFRelease(appUrlRef);
CFRelease(macPath);

但是,对于更简单的东西来说有什么优点:

However, what is the advantage of that over something simpler, like the following:

QDir dir = QDir(QCoreApplication::applicationDirPath());
dir.cdUp();
dir.cdUp();
return dir;


推荐答案

Qt 5和OS X 10.9更大的是:

The modern way with Qt 5 and OS X 10.9 or greater is:

CFURLRef url = (CFURLRef)CFAutorelease((CFURLRef)CFBundleCopyBundleURL(CFBundleGetMainBundle()));
QString path = QUrl::fromCFURL(url).path();

这篇关于Qt - 访问束路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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