【发布时间】:2011-01-09 17:53:33
【问题描述】:
我正在编写一个需要写入已安装目录 (C:\Program Files...) 的 Qt 程序(Windows 7 最初为 4.7)。当我尝试写入将被“保护”的位置(程序文件、C:\ 等)时,没有创建任何文件。但是,QFile 没有给我任何错误代码(error() 返回 0,这意味着它工作正常)。
这是我正在使用的代码片段,但它不起作用。我将在程序的后面关闭文件。
QApplication a(argc, argv);
// Setting plugin paths.
QStringList paths = QCoreApplication::libraryPaths();
paths.append(QCoreApplication::applicationDirPath());
QCoreApplication::setLibraryPaths(paths);
// Debug file.
QString path = QCoreApplication::applicationDirPath() + "/debug.dat";
//QFile debugFile(QCoreApplication::applicationDirPath() + "/debug.dat");
QFile debugFile("C:/debug.txt");
qDebug() << debugFile.error();
debugFile.setPermissions(QFile::WriteUser | QFile::WriteGroup | QFile::WriteOwner | QFile::WriteOther);
debugFile.open(QFile::WriteOnly);
QTextStream debugStream(&debugFile);
// Processing the arguments.
debugStream << QString("Processing Arguments\n");
有人对如何解决这个问题有任何提示吗?
感谢您的帮助,
杰克
添加清单文件是我解决此问题的途径。
感谢大家的帮助。
【问题讨论】:
-
“但是,QFile 没有给我任何错误代码”:您应该检查 debugFile.open() 的返回值。