Qt获取路径

1、获取执行文件路径

     #include <QCoreApplication.h>
     QCoreApplication::applicationDirPath();


2、获取工程路径

     #include <qdir.h>
     QDir dir;
     QString pathDB = dir.currentPath();


在Linux系统下使用QT获取路径有不同的场景和方法,以下为你介绍一些常见的情况及对应代码示例: ### 获取DLL文件路径 在DLL中获取DLL本身的路径以及调用DLL的文件的路径,可通过`GetModuleFileName(HMODULE hModule, LPTSTR lpFilename, DWORD nSize)`函数来获取,根据`hModule`参数来辨别是DLL本身还是DLL的调用者[^2]。示例代码如下: ```cpp #include <windows.h> #include <QDebug> void getDllPaths() { TCHAR dllPath[MAX_PATH]; TCHAR callerPath[MAX_PATH]; // 获取DLL本身的路径 GetModuleFileName(NULL, dllPath, MAX_PATH); qDebug() << "DLL Path:" << QString::fromWCharArray(dllPath); // 获取调用DLL的文件的路径 GetModuleFileName(GetModuleHandle(NULL), callerPath, MAX_PATH); qDebug() << "Caller Path:" << QString::fromWCharArray(callerPath); } ``` ### 获取指定磁盘信息相关路径 可以通过读取`/proc/mounts`文件或执行`df -k`命令来获取指定磁盘信息,进而获取相关路径。以下是两种方式的示例代码: ```cpp #include <QFile> #include <QProcess> #include <QStringList> #include <QDebug> // 方式一:读取 /proc/mounts 文件 bool getDiskInfoFromProcMounts() { QFile file("/proc/mounts"); file.open(QIODevice::ReadOnly); QString strMountInfo = file.readAll(); qDebug() << strMountInfo; file.close(); QStringList list = strMountInfo.split("\n"); for(int i = 0; i < list.count(); i++) { if(list[i].startsWith("/dev/sd", Qt::CaseInsensitive)) { qDebug() << "find disk"; return true; } } return false; } // 方式二:执行 df -k 命令 bool getDiskInfoFromProcess() { QProcess m_process; QString cmd = "df -k"; QString m_result; qDebug() << cmd; m_process.start(cmd); qDebug("start "); m_process.waitForFinished(); m_result = m_process.readAll(); qDebug() << m_result; if(m_result.length() < 0) { // 处理错误情况 } QStringList list = m_result.split("\n"); qDebug() << list; for(int i = 0; i < list.count(); i++) { if(list[i].startsWith("/dev/sd", Qt::CaseInsensitive)) { qDebug() << "find disk"; return true; } } return false; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值