Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encoding in QProcess output
Forum Updated to NodeBB v4.3 + New Features

Encoding in QProcess output

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 6 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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 ?

    ODБOïO KroMignonK 2 Replies Last reply
    0
    • ODБOïO ODБOï

      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 ?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      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 ?

      jsulmJ Pablo J. RoginaP 2 Replies Last reply
      0
      • ODБOïO ODБOï

        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 ?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @LeLev Did you try

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

        ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        ODБOïO 1 Reply Last reply
        1
        • ODБOïO ODБOï

          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 ?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

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

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

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          1
          • jsulmJ jsulm

            @LeLev Did you try

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

            ?

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by ODБOï
            #5

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

            jsulmJ KroMignonK aha_1980A 3 Replies Last reply
            0
            • ODБOïO ODБOï

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              ODБOïO 1 Reply Last reply
              0
              • ODБOïO ODБOï

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

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #7

                @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;
                            }
                        });
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                ODБOïO 1 Reply Last reply
                0
                • jsulmJ jsulm

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

                  ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by
                  #8

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

                  VRoninV 1 Reply Last reply
                  0
                  • ODБOïO ODБOï

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

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    2
                    • KroMignonK KroMignon

                      @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;
                                  }
                              });
                      
                      ODБOïO Offline
                      ODБOïO Offline
                      ODБOï
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • ODБOïO ODБOï

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

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @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());
                        });
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        ODБOïO 1 Reply Last reply
                        3
                        • VRoninV VRonin

                          @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());
                          });
                          
                          ODБOïO Offline
                          ODБOïO Offline
                          ODБOï
                          wrote on last edited by
                          #12

                          @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

                          1 Reply Last reply
                          2
                          • ODБOïO ODБOï

                            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 ?

                            Pablo J. RoginaP Offline
                            Pablo J. RoginaP Offline
                            Pablo J. Rogina
                            wrote on last edited by
                            #13

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

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            ODБOïO 1 Reply Last reply
                            3
                            • Pablo J. RoginaP Pablo J. Rogina

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

                              ODБOïO Offline
                              ODБOïO Offline
                              ODБOï
                              wrote on last edited by ODБOï
                              #14

                              @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 ?

                              1 Reply Last reply
                              0
                              • ODБOïO Offline
                                ODБOïO Offline
                                ODБOï
                                wrote on last edited by
                                #15

                                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

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved