简体   繁体   English

QT 调试 QFile::remove() Windows 10 MSVS 2019

[英]QT Debugging QFile::remove() Windows 10 MSVS 2019

I am working on a QT application that has to communicate with a few Windows-utilities.我正在开发一个 QT 应用程序,它必须与一些 Windows 实用程序进行通信。 The result of these utilities are a couple of “production-files” that should be listed in a “filenames-txt-file” for further use.这些实用程序的结果是几个“生产文件”,应列在“filenames-txt-file”中以供进一步使用。

The production of such a “filenames-txt-file” with the list of “production-files” is done with QT using QFileInfo.使用 QFileInfo 使用 QT 生成带有“production-files”列表的“filenames-txt-file”。
Sometimes an old “filenames-txt-file” already exists in the working-directory and should be removed before it can be created with the new results.有时,旧的“filenames-txt-file”已经存在于工作目录中,应该在使用新结果创建它之前将其删除。

Here is the problem:这是问题所在:
QFile::remove("somefile") does not work while debugging. QFile::remove("somefile") 在调试时不起作用。
It works fine, when I run the Exe in the debug-folder outside MSVS,它工作正常,当我在 MSVS 之外的调试文件夹中运行 Exe 时,
and it works fine running the release version.并且运行发布版本时运行良好。

While debugging, I get this messages:调试时,我收到以下消息:

if (QFile::exists(filenameFull)) {
    QFile f (filenameFull);     
    qDebug() << f.remove(filenameFull);     // returns false
    qDebug() << f.errorString();            // returns “unknown error”
}

I elevated Microsoft Visual Studio 2019 to run as administrator.我将 Microsoft Visual Studio 2019 提升为以管理员身份运行。
I did set the UAC execution level to “highestAvailable”.我确实将 UAC 执行级别设置为“highestAvailable”。
Is anything else needed to make this code working while debugging?调试时是否需要其他任何东西才能使此代码正常工作?

It may be unrelated but instead f.remove(filenameFull) you can just use f.remove() or QFile::remove(filenameFull) .它可能不相关,但f.remove(filenameFull)您可以只使用f.remove()QFile::remove(filenameFull)

QFile::remove on windows uses winapi function DeleteFileW(("\\\\?\\" + QDir::toNativeSeparators(filenameFull)).utf16()) where "\\?\" is used to avoid MAX_PATH limitation. QFile::remove on windows 使用 winapi function DeleteFileW(("\\\\?\\" + QDir::toNativeSeparators(filenameFull)).utf16())其中"\\?\"用于避免 MAX_PATH 限制。 Try calling this function directly and analyze result.尝试直接调用此 function 并分析结果。

You are calling static function, QFile::remove(QString filename) , so f does not see the error.您正在调用 static function, QFile::remove(QString filename) ,所以f没有看到错误。 So try this:所以试试这个:

if (QFile::exists(filenameFull)) {
    QFile f (filenameFull);
    qDebug() << f.exists();
    qDebug() << f.remove();      
    qDebug() << f.errorString();
}

That should solve the part about not getting error string.那应该解决关于没有得到错误字符串的部分。

Solved thanks to @hyde.感谢@hyde 解决了。 The issue was indeed that filenameFull was opened all the time.问题确实是filenameFull一直被打开。
Not in the code itself, but in the Configuration Properties -> Debugging -> Command Arguments不在代码本身,而是在Configuration Properties -> Debugging -> Command Arguments
It was hanging there from an earlier stage in the coding process.它在编码过程的早期阶段就挂在那里了。
It explains as well why running the EXE outside the debugging-process was running fine.它还解释了为什么在调试过程之外运行 EXE 运行良好。
Lesson: keep my coding steps clear at all times....课程:始终保持我的编码步骤清晰......
Thanks for getting me back on track.谢谢你让我回到正轨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM