Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProcess does not recognize input path with accent



  • Hello,

    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).

    The environnement of execution and development is 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.
    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.

    #include <QCoreApplication>
    #include <Qdebug>
    #include <QObject>
    #include <QProcess>
    #include <QDir>
    #include <wtypes.h>
    #include <winbase.h>
    
    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.

    This is the part that I copy/paste in the bigger project and which I now execute via a button click in QT.
    And this time, the QProcess doesn't recognize the accentuated path (sometimes it creates a file with name like this 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.
    I would like to understand why the QProcess instance behaves differently when integrated within a bigger project.
    What may impact the behavior of the QProcess and leads it to treat differently the input in the second case?

    I thank you in advance for your help.


  • Lifetime Qt Champion

    @xchess64 Is the source code file encoded as UTF-8?



  • @xchess64
    I don't know why the difference, I would suspect something about codecs/code pages/locales different in the big program?

    But just before you start investigating: is your problem/need to use QProcess only for this "DOS copy command"? Because unless you need it for all sorts of other commands, I wouldn't copy a file via some QProcess command anyway. So if that's all you could save yourself a lot of grief... :)



  • @JonB Thanks guys for the response.
    Indeed, the QProcess is needed for more things (such as getting feedback and percentage of operations). The copy is only to isolate the problem. In reality, I do much more things.



  • @jsulm
    I have checked, Indeed the example code is encoded in UTF-8 and the code of my bigger project is encoded in ANSI. This may explain why it works in the first cas and not in the second case.
    Remark: I may not be allowed to change the encoding of the bigger project.



  • @xchess64 said in QProcess does not recognize input path with accent:

    Remark: I may not be allowed to change the encoding of the bigger project.

    Then --- and I never know the exact code for this! --- do you need to not put a literal accentuated-character (é) into your source code file but instead use whatever it is which correctly encodes the desired character (like some \x...)? @jsulm ?


  • Lifetime Qt Champion

    @JonB I don't think so, as encoding is ANSI which is 7bit. But I'm not sure.


  • Lifetime Qt Champion

    @JonB: yes, and then use QString::fromUtf8()