Qt相对路径和绝对路径转换

 

#include <QDir>
#include <QString>
#include <QDebug>
 
int main() {
    // 当前工作目录
    QDir currentDir;
    qDebug() << "当前工作目录:" << currentDir.current();
 
    // 相对路径转绝对路径
    QString relativePath = "./example/test.txt";
    QString absolutePath = currentDir.absoluteFilePath(relativePath);
    qDebug() << "相对路径转绝对路径:" << absolutePath;
 
    // 绝对路径转相对路径
    QString absolutePath2 = "/home/user/example/test.txt";
    QString relativePath2 = currentDir.relativeFilePath(absolutePath2);
    qDebug() << "绝对路径转相对路径:" << relativePath2;
 
    return 0;
}

 

posted @ 2024-05-16 18:44  远方是什么样子  阅读(129)  评论(0编辑  收藏  举报