在QT开发中,总是绕不开需要获取各种路径,本文整理了常用的路径获取,代码如下:#include #include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //获取程序所在路径 QString applicationDirPath = QCoreApplication::applicationDirPath(); qDebug() << "applicationDirPath=" << applicationDirPath; //程序的完整路径 QString applicationFilePath = qApp->applicationFilePath(); qDebug() << "applicationFilePath=" << applicationFilePath; //当前工作目录 QString currentPath = QDir::currentPath(); qDebug() << "currentPath=" << currentPath; //用户目录路径 QString HomeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); qDebug() << "HomeLocation=" << HomeLocation; QStringList HomeLocation2 = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); qDebug() << "HomeLocation2=" << HomeLocation2[0]; //我的文档路径 QString DocumentsLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); qDebug() << "DocumentsLocation=" << DocumentsLocation; QStringList DocumentsLocation2 = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); qDebug() << "DocumentsLocation2=" << DocumentsLocation2[0]; //桌面路径 QString DesktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); qDebug() << "DesktopLocation=" << DesktopLocation; QStringList DesktopLocation2 = QStandardPaths::standardLocations(QStandardPaths::DesktopLocation); qDebug() << "DesktopLocation2=" << DesktopLocation2[0]; //程序数据存放路径 QString AppDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); qDebug() << "AppDataLocation=" << AppDataLocation; QStringList AppDataLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation); qDebug() << "AppDataLocation2=" << AppDataLocation2[0]; /*Qt5.5 中引入了另一种方法*/ QString AppConfigLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation); qDebug() << "AppConfigLocation=" << AppConfigLocation; QStringList AppConfigLocation2 = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation); qDebug() << "AppConfigLocation2=" << AppConfigLocation2[0]; //临时文件路径 QString TempLocation = QStandardPaths::writableLocation(QStandardPaths::TempLocation); qDebug() << "TempLocation=" << TempLocation; QStringList TempLocation2 = QStandardPaths::standardLocations(QStandardPaths::TempLocation); qDebug() << "TempLocation2=" << TempLocation2[0]; //更传统的方法是利用QDir的一个静态函数tempPath() QString tempPath = QDir::tempPath(); qDebug() << "tempPath=" << tempPath; system("pause"); return a.exec(); }
输出结果如下:applicationDirPath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug" applicationFilePath= "C:/Users/Administrator/Desktop/QtPathsTest/Debug/QtPathsTest.exe" currentPath= "C:/Users/Administrator/Desktop/QtPathsTest/QtPathsTest" HomeLocation= "C:/Users/Administrator" HomeLocation2= "C:/Users/Administrator" DocumentsLocation= "C:/Users/Administrator/Documents" DocumentsLocation2= "C:/Users/Administrator/Documents" DesktopLocation= "C:/Users/Administrator/Desktop" DesktopLocation2= "C:/Users/Administrator/Desktop" AppDataLocation= "C:/Users/Administrator/AppData/Roaming/QtPathsTest" AppDataLocation2= "C:/Users/Administrator/AppData/Roaming/QtPathsTest" AppConfigLocation= "C:/Users/Administrator/AppData/Local/QtPathsTest" AppConfigLocation2= "C:/Users/Administrator/AppData/Local/QtPathsTest" TempLocation= "C:/Users/Administrator/AppData/Local/Temp" TempLocation2= "C:/Users/Administrator/AppData/Local/Temp" tempPath= "C:/Users/Administrator/AppData/Local/Temp"
联系客服