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; }
浙公网安备 33010602011771号