简体   繁体   English

带重音符号的 QProcess 输入字符串

[英]QProcess input string with accents

I have two programs each with QProcess and I have a different behavior concerning the QProcess input with accentuated characters (more precisely I create a Qprocess to execute a dos copy command and the path takes accent).我有两个程序,每个程序都带有 QProcess,并且我对带有重音字符的 QProcess 输入有不同的行为(更准确地说,我创建了一个 Qprocess 来执行 dos copy命令并且路径带有重音符号)。

The environnement of execution and development is Windows 10.执行和开发环境为Windows 10。

The first program is a simple prototype that I made to test if my code can work correctly.第一个程序是一个简单的原型,用于测试我的代码是否可以正常工作。

Here is the prototype code I have, in which the copy works correctly, integrated in a simple main() function.这是我的原型代码,其中副本可以正常工作,集成在一个简单的main() function 中。 The code is supposed to copy a file named sfx.exe into a path with accent F:\path_accentué and it indeed does the copy correctly.该代码应该将名为sfx.exe的文件复制到带有重音F:\path_accentué的路径中,并且它确实正确地进行了复制。

#include <QCoreApplication>
#include <Qdebug>
#include <QObject>
#include <QProcess>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QProcess* processus = new QProcess();
    QStringList args; 
    QString path("F:\\path_accentué");
    
    args << "/C" << "copy" << "E:\\test\\sfx.exe" << path;
    processus->start("cmd.exe", args);  

    if (!processus->waitForStarted())
    {
        qDebug() << "Could not launch the process";
    }
    //processus->write(s.c_str());
    if (!processus->waitForFinished(-1))
    {
        qDebug() << "Finished";
    }
    delete processus;
    return app.exec();
}

However, when I integrate (literally copies and pasted without changing) this prototype within a bigger code project, my instance of QProcess does not recognize the accentuated path, as if accents are no more supported.但是,当我在一个更大的代码项目中集成(实际上是复制和粘贴而不更改)这个原型时,我的QProcess实例无法识别accentuated路径,就好像不再支持重音一样。

This is the part that I copy/paste in the bigger project and which I now execute via a button click in QT.这是我在更大的项目中复制/粘贴的部分,现在我通过单击 QT 中的按钮来执行。 And this time, the QProcess doesn't recognize the accentuated path (Instead it creates a file with name like this path_accentu� )而这一次,QProcess 无法识别重读路径(相反,它会创建一个名称类似于path_accentu� 的文件)

            QProcess* processus = new QProcess();
            QStringList args; 
            QString path("F:\\path_accentué");
            args << "/C" << "copy" << "E:\\test\\sfx.exe" << path;            processus->start("cmd.exe", args);  
            if (!processus->waitForStarted())
            {
                        qDebug() << "Could not launch the process";
            }
            //processus->write(s.c_str());
            if (!processus->waitForFinished(-1))
            {
                        qDebug() << "Finished";
            }

I do not find in the documentation a way to force the QProcess to recognize accentuated inputs.我在文档中找不到强制QProcess识别加重输入的方法。 I would like to understand why the QProcess instance behaves differently when integrated within my bigger project.我想了解为什么 QProcess 实例在集成到我的更大项目中时表现不同。 What may impact the behavior of the QProcess and leads it to treat differently the input in the second case?什么可能会影响 QProcess 的行为并导致它在第二种情况下以不同的方式处理输入?

Note: The QProcess is needed for more things but not only for the copy (such as getting feedback and percentage of operations).注意: QProcess 需要更多的东西,但不仅仅是副本(例如获取反馈和操作百分比)。 The copy is only to isolate the problem.复制只是为了隔离问题。 In reality, I do much more things.实际上,我做的事情要多得多。

I tried to recreate your behaviour with Qt 5.15 and could create a file with accent with我尝试使用 Qt 5.15 重新创建您的行为,并且可以创建一个带有重音的文件

  • start("cmd",{args...})
  • start("cmd /c args...")
  • setNativeArguments("/c args...") + start("cmd") setNativeArguments("/c args...") + start("cmd")

Last one is recommended for "cmd" calls, see the remarks here:最后一个建议用于“cmd”调用,请参见此处的备注:
https://doc.qt.io/qt-5/qprocess.html#start https://doc.qt.io/qt-5/qprocess.html#start

The only thing, which did not work, because it deadlocks was唯一不起作用的,因为它死锁是

  • setArguments({args...}) + start("cmd") setArguments({args...}) + start("cmd")

Demo here:演示在这里:
https://gist.github.com/elsamuko/59f110cf3a678beae9db27860f6305c9 https://gist.github.com/elsamuko/59f110cf3a678beae9db27860f6305c9

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

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