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.8k 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.
  • O ODБOï
    7 Jun 2019, 10:01

    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 ?

    O Offline
    O Offline
    ODБOï
    wrote on 7 Jun 2019, 10:05 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 ?

    J P 2 Replies Last reply 7 Jun 2019, 10:21
    0
    • O ODБOï
      7 Jun 2019, 10:05

      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 ?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Jun 2019, 10:21 last edited by
      #3

      @LeLev Did you try

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

      ?

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

      O 1 Reply Last reply 7 Jun 2019, 10:23
      1
      • O ODБOï
        7 Jun 2019, 10:01

        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 ?

        K Offline
        K Offline
        KroMignon
        wrote on 7 Jun 2019, 10:23 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
        • J jsulm
          7 Jun 2019, 10:21

          @LeLev Did you try

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

          ?

          O Offline
          O Offline
          ODБOï
          wrote on 7 Jun 2019, 10:23 last edited by ODБOï 6 Jul 2019, 10:26
          #5

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

          J K A 3 Replies Last reply 7 Jun 2019, 10:27
          0
          • O ODБOï
            7 Jun 2019, 10:23

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 7 Jun 2019, 10:27 last edited by
            #6

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

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

            O 1 Reply Last reply 7 Jun 2019, 10:30
            0
            • O ODБOï
              7 Jun 2019, 10:23

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

              K Offline
              K Offline
              KroMignon
              wrote on 7 Jun 2019, 10:30 last edited by KroMignon 6 Jul 2019, 10:31
              #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)

              O 1 Reply Last reply 7 Jun 2019, 10:34
              0
              • J jsulm
                7 Jun 2019, 10:27

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

                O Offline
                O Offline
                ODБOï
                wrote on 7 Jun 2019, 10:30 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.

                V 1 Reply Last reply 7 Jun 2019, 10:35
                0
                • O ODБOï
                  7 Jun 2019, 10:23

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

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 7 Jun 2019, 10:33 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
                  • K KroMignon
                    7 Jun 2019, 10:30

                    @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;
                                }
                            });
                    
                    O Offline
                    O Offline
                    ODБOï
                    wrote on 7 Jun 2019, 10:34 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
                    • O ODБOï
                      7 Jun 2019, 10:30

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

                      V Offline
                      V Offline
                      VRonin
                      wrote on 7 Jun 2019, 10:35 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

                      O 1 Reply Last reply 7 Jun 2019, 10:42
                      3
                      • V VRonin
                        7 Jun 2019, 10:35

                        @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());
                        });
                        
                        O Offline
                        O Offline
                        ODБOï
                        wrote on 7 Jun 2019, 10:42 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
                        • O ODБOï
                          7 Jun 2019, 10:05

                          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 ?

                          P Offline
                          P Offline
                          Pablo J. Rogina
                          wrote on 7 Jun 2019, 13:23 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

                          O 1 Reply Last reply 7 Jun 2019, 16:09
                          3
                          • P Pablo J. Rogina
                            7 Jun 2019, 13:23

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

                            O Offline
                            O Offline
                            ODБOï
                            wrote on 7 Jun 2019, 16:09 last edited by ODБOï 6 Jul 2019, 16:11
                            #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
                            • O Offline
                              O Offline
                              ODБOï
                              wrote on 7 Jun 2019, 16:29 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

                              11/15

                              7 Jun 2019, 10:35

                              • Login

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