Encoding in QProcess output



  • Hi,
    I run a .bat script using QProcess and get its output but there is character encoding issues
    ex : i got '?' instead of 'é'

            process = new QProcess(this);
            process->start(PROSS, QStringList() << "/k" << "title" << "checking..."); 
    
            QObject::connect(process,&QProcess::errorOccurred,process,[&](const QProcess::ProcessError &error){
                qDebug()<<process->errorString();
            });
    
            QObject::connect(process,&QProcess::readyReadStandardOutput,process,[&](){
                QString output(process->readAllStandardOutput());
                //output = output.toUtf8();
                QStringList lines = output.split("\\r\\n");
                QStringListIterator it(lines);
                while(it.hasNext()){
                   qDebug() << it.next().toLocal8Bit().constData()<< endl;
                }
            });
    

    How to get proper output please ?



  • if i run my windows terminal directly and execute

    ipconfig/all >> text.txt

    it creates a text file with lot of encoding issues also.

    Opening that file in Notepad++ and testing different encodings does not help
    is this a problem that is more related to windoz terminal itself maybe ?


  • Qt Champions 2018

    @LeLev Did you try

    qDebug() << it.next() << endl;
    

    ?



  • @LeLev I think it would be better to get message this way:

    QString output = QString::fromLocal8Bit(process->readAllStandardOutput());
    


  • @jsulm yes, but got same
    @KroMignon i also tryed
    QString output = QString::fromLocal8Bit(process->readAllStandardOutput());
    but still same output


  • Qt Champions 2018

    @LeLev Try with std::cout instead of qDebug()



  • @LeLev Other thing I can suggest is to use qUtf8Printable()

     QObject::connect(process,&QProcess::readyReadStandardOutput,process,[&](){
                QString output = QString::fromLocal8Bit(process->readAllStandardOutput());
                QStringList lines = output.split("\\r\\n");
                QStringListIterator it(lines);
                while(it.hasNext()){
                   qDebug() << qUtf8Printable(it.next())<< endl;
                }
            });
    


  • @jsulm i have never used it with Qt,i will try?My final aim is to write the output to a file.


  • Qt Champions 2018

    @LeLev said in Encoding in QProcess output:

    process->readAllStandardOutput())

    To debug this, I'd suggest to print the output as hex:

    qDebug() << process->readAllStandardOutput().toHex();

    Then you need to find then code for 'é', which allows you to find the encoding used by your system. It may well be local 8 bit, UTF-8 or any of the encodings we used in the 1990. Before you don't know the exact codec, you cannot properly convert the output to a QString and therefore you cannot print it.

    Regards



  • @KroMignon unfortunately the output is almost the same,.. i had :
    ? in place of é
    now i have
    , in place of é


  • Qt Champions 2018

    @LeLev said in Encoding in QProcess output:

    My final aim is to write the output to a file.

    Do you need to change encoding between reading and writing? if the answer is no you can just simply:

     QObject::connect(process,&QProcess::readyReadStandardOutput,process,[=](){
    QFile outputLog("outputLog.txt");
    if(!outputLog.open(QIODevice::Append | QIODevice::Text))
    return;
    outputLog.write(process->readAllStandardOutput());
    });
    


  • @VRonin no i don't neet to change, i only want the output of command
    ipconfig /all
    inside a new file.
    but sadly there is still issues in the file (outputLog.txt)..

    I will try do debug this using what @aha_1980 said. On Monday. This is first time Qt, windows and coding pissed me off for real.

    Thank you very much for help anyway



  • @LeLev said in Encoding in QProcess output:

    if i run my windows terminal directly and execute
    ipconfig/all >> text.txt
    it creates a text file with lot of encoding issues also.

    Given that, should you set the proper encoding for the command line? See this post as it may help...



  • @Pablo-J.-Rogina said in Encoding in QProcess output:

    may help...

    not realy ..

    i tryed

    REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 0xfde9
    

    failed, then :

    
    Now we need to add a TT font that supports the characters you need like Courier New, we do this by adding zeros to the string name, so in this case the next one would be "000" :
    
        REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont" /v 000 /t REG_SZ /d "Courier New"
    Now we implement UTF-8 support:
    
        REG ADD HKCU\Console /v CodePage /t REG_DWORD /d 65001 /f
    Set default font to "Courier New":
    
        REG ADD HKCU\Console /v FaceName /t REG_SZ /d "Courier New" /f
    Set font size to 20 :
    
        REG ADD HKCU\Console /v FontSize /t REG_DWORD /d 20 /f
    
    

    failed, then :

    Save the following into a file with ".reg" suffix and execute
    
    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
    "CodePage"=dword:0000fde9
    

    now if i open my terminal (cmd.exe) it will close instantly : any idea why ?



  • actually if i run cmd like this :
    cmd /K chcp 65001
    it will not close instantly and output is correct now.
    So kind of solved, but broked "normal openning".. also if i type somethig wrong in it, i dont have error message, instead it closed immediatly


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.