You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Qt调用Notepad打开文本文件报错,寻求解决方法

Qt调用记事本打开文件报错的解决方法

你当前的代码QProcess::startDetached("C:\\Windows\\system32\\notepad.exe", { ":/notes.txt" });会报错,核心原因是:":/notes.txt"是Qt专属的资源路径,记事本作为系统外部程序,无法识别Qt打包在二进制文件里的资源,它只能访问本地文件系统中存在的真实文件路径。

解决办法

方法1:将资源文件导出到临时文件后打开

利用Qt的临时文件类,把资源里的内容写入本地临时文件,再让记事本打开这个临时文件:

QFile resFile(":/notes.txt");
if (resFile.open(QIODevice::ReadOnly)) {
    QTemporaryFile tempFile;
    if (tempFile.open()) {
        tempFile.write(resFile.readAll());
        tempFile.flush();
        // 调用记事本打开临时文件
        QProcess::startDetached("notepad.exe", { tempFile.fileName() });
    }
    resFile.close();
}

临时文件会在程序退出时自动删除,无需手动清理。

方法2:直接使用本地文件路径

如果你的notes.txt可以提前部署到程序运行目录(比如和exe同文件夹),直接用本地路径调用:

QString localPath = QCoreApplication::applicationDirPath() + "/notes.txt";
QProcess::startDetached("notepad.exe", { localPath });

内容的提问来源于stack exchange,提问作者temp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:50:40