从 QFile 获取文件名?

发布于 2024-09-18 12:54:39 字数 112 浏览 2 评论 0原文

例如:

QFile f("/home/umanga/Desktop/image.jpg");

我如何只获取文件名 - “image.jpg”?

eg:

QFile f("/home/umanga/Desktop/image.jpg");

How I get only the filename - "image.jpg"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏佲洛 2024-09-25 12:54:39

使用 QFileInfo 删除路径(如果有):

QFileInfo fileInfo(f.fileName());
QString filename(fileInfo.fileName());

Use a QFileInfo to strip out the path (if any):

QFileInfo fileInfo(f.fileName());
QString filename(fileInfo.fileName());
初吻给了烟 2024-09-25 12:54:39

一种方法,不一定是最好的:从 QFile 中,您可以使用 QFile::fileName()

QFile f("/home/umanga/Desktop/image.jpg");
QString str = f.fileName();

那么你可以使用字符串功能,例如 QString::split

QStringList parts = str.split("/");
QString lastBit = parts.at(parts.size()-1);

One approach, not necessarily the best: from a QFile, you can get the file specification with QFile::fileName():

QFile f("/home/umanga/Desktop/image.jpg");
QString str = f.fileName();

then you can just use the string features like QString::split:

QStringList parts = str.split("/");
QString lastBit = parts.at(parts.size()-1);
牛↙奶布丁 2024-09-25 12:54:39

另外:要分隔具有 QFile f 的文件名和文件路径,

QString path = f.fileName();
QString file = path.section("/",-1,-1);
QString dir = path.section("/",0,-2);

您不需要创建额外的 fileInfo。

just in addition: to seperate filename and file path having QFile f

QString path = f.fileName();
QString file = path.section("/",-1,-1);
QString dir = path.section("/",0,-2);

you don't need to create an additional fileInfo.

梦醒灬来后我 2024-09-25 12:54:39

我用这个:

bool utes::pathsplit(QString source,QString *path,QString *filename)
{
QString fn;
int index;
    if (source == "") return(false);
    fn = source.section("/", -1, -1);
    if (fn == "") return(false);
    index = source.indexOf(fn);
    if (index == -1) return(false);
    *path = source.mid(0,index);
    *filename = fn;
    return(true);
}

I use this:

bool utes::pathsplit(QString source,QString *path,QString *filename)
{
QString fn;
int index;
    if (source == "") return(false);
    fn = source.section("/", -1, -1);
    if (fn == "") return(false);
    index = source.indexOf(fn);
    if (index == -1) return(false);
    *path = source.mid(0,index);
    *filename = fn;
    return(true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文