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. doesn't print the ( µ ) symbol correctly in qt
Forum Updated to NodeBB v4.3 + New Features

doesn't print the ( µ ) symbol correctly in qt

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 717 Views 2 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.
  • Ramkumar MohanR Offline
    Ramkumar MohanR Offline
    Ramkumar Mohan
    wrote on last edited by
    #1

    Hi,
    I am usning Thermal printer doesn't print the ( µ ) symbol correctly ,

    My code :-

     void MainWindow::on_pushButton_clicked()
     {
           QString teststr3 = QString().fromLatin1("\xb5");
           Printer *p = new Printer();
           std::cout << "Trying to open port" << std::endl;
           bool res = p->open("/dev/ttyS0");
           std::cout << "Status: " << res << std::endl;
           if (!res)
           {
                 std::cerr << "Error opening port, aborting" << std::endl;
                 return (0);
            }
            p->feed();
            p->setAlign(Printer::LEFT);
            p->write("  symbol    : "   + teststr3);
            p->feed();
            p->close();
            return 1;
      }
    

    Output:-

    WhatsApp Image 2023-04-13 at 11.19.01 AM.jpeg

    how to fit it....

    C 1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, your printer does not seem to like UTF8/Unicode and instead outputs it as 2 characters in the Code Page 437.
      So either switch your printer to Unicode/UTF8 mode or change your teststr3 to the character \xE6 ( µ in Code Page 437).
      Perhaps easiest is try something like:

      char c = '\xE6';
      p->write(c);
      
      Ramkumar MohanR 1 Reply Last reply
      0
      • hskoglundH hskoglund

        Hi, your printer does not seem to like UTF8/Unicode and instead outputs it as 2 characters in the Code Page 437.
        So either switch your printer to Unicode/UTF8 mode or change your teststr3 to the character \xE6 ( µ in Code Page 437).
        Perhaps easiest is try something like:

        char c = '\xE6';
        p->write(c);
        
        Ramkumar MohanR Offline
        Ramkumar MohanR Offline
        Ramkumar Mohan
        wrote on last edited by
        #3

        @hskoglund said in doesn't print the ( µ ) symbol correctly in qt:

        char c = '\xE6';
        p->write(c);

        I tried this way but it keeps getting junk.

          int MainWindow::on_Logo_Btn_3_clicked()
          {
        
             char data = '\xb5';
             qDebug()<<"Data : "<<data;
             Printer *p = new Printer();
             std::cout << "Trying to open port" << std::endl;
             bool res = p->open("/dev/ttyS0");
             std::cout << "Status: " << res << std::endl;
             if (!res) 
             { 
                   std::cerr << "Error opening port, aborting" << std::endl;
                   return (0);
             }
             p->reset();
             p->setAlign(Printer::MIDDLE);
             p->setBold(true);
             p->writes(&data);
             p->setBold(false);
             p->feed();
             p->feed();
             p->setAlign(Printer::LEFT);
             p->close();
             return 1;
         }
        

        WhatsApp Image 2023-04-13 at 7.37.20 PM.jpeg

        piervalliP 1 Reply Last reply
        0
        • Ramkumar MohanR Ramkumar Mohan

          @hskoglund said in doesn't print the ( µ ) symbol correctly in qt:

          char c = '\xE6';
          p->write(c);

          I tried this way but it keeps getting junk.

            int MainWindow::on_Logo_Btn_3_clicked()
            {
          
               char data = '\xb5';
               qDebug()<<"Data : "<<data;
               Printer *p = new Printer();
               std::cout << "Trying to open port" << std::endl;
               bool res = p->open("/dev/ttyS0");
               std::cout << "Status: " << res << std::endl;
               if (!res) 
               { 
                     std::cerr << "Error opening port, aborting" << std::endl;
                     return (0);
               }
               p->reset();
               p->setAlign(Printer::MIDDLE);
               p->setBold(true);
               p->writes(&data);
               p->setBold(false);
               p->feed();
               p->feed();
               p->setAlign(Printer::LEFT);
               p->close();
               return 1;
           }
          

          WhatsApp Image 2023-04-13 at 7.37.20 PM.jpeg

          piervalliP Offline
          piervalliP Offline
          piervalli
          wrote on last edited by
          #4

          @Ramkumar-Mohan
          If I see corretly, the printer is a retail printer, usually they have a single code page. You should check before the printer.

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by hskoglund
            #5

            Hi, because you're using p->writes(&data); I think you need a terminating '\x00' zero byte also. (Since there's none that's why you get all those junk characters).
            So try

            char data[2] = { '\xE6', '\x00' };
            ... // and then later
            p->writes(data);
            

            Edit: I think if you want to print a µ and not a ╡ you should use '\xE6', not '\xb5' more here https://en.wikipedia.org/wiki/Code_page_437

            1 Reply Last reply
            1
            • Ramkumar MohanR Ramkumar Mohan

              Hi,
              I am usning Thermal printer doesn't print the ( µ ) symbol correctly ,

              My code :-

               void MainWindow::on_pushButton_clicked()
               {
                     QString teststr3 = QString().fromLatin1("\xb5");
                     Printer *p = new Printer();
                     std::cout << "Trying to open port" << std::endl;
                     bool res = p->open("/dev/ttyS0");
                     std::cout << "Status: " << res << std::endl;
                     if (!res)
                     {
                           std::cerr << "Error opening port, aborting" << std::endl;
                           return (0);
                      }
                      p->feed();
                      p->setAlign(Printer::LEFT);
                      p->write("  symbol    : "   + teststr3);
                      p->feed();
                      p->close();
                      return 1;
                }
              

              Output:-

              WhatsApp Image 2023-04-13 at 11.19.01 AM.jpeg

              how to fit it....

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @Ramkumar-Mohan said in doesn't print the ( µ ) symbol correctly in qt:

              To expand on @hskoglund's input.

              QString teststr3 = QString().fromLatin1("\xb5");
              This puts a 16-bit number containing 0x00b5 into the QString. This is UTF-16 form of the character you provided from the ISO-8859-1 (Latin1) set.

              p->write(" symbol : " + teststr3);
              This code will probably invoke this operator to produce a QString containing both parts.

              You do not say what the write() function is expecting so I am guessing it is a crude char*. The QString will be cast to char* using UTF-8 encoding by default. This generates two bytes for the last character in the QString:

              • 0xC2 -> Which is the ┬ character in the 8-bit Code Page 437
              • 0xB5 -> Which is the ╡character in the Code Page 437

              Those are the characters you see on the paper.
              To get a µ in the code page (on that printer) you need to send a 0xe6 byte.

              You can deliberately convert your QString to a 8-bit string in the correct encoding using QTextCodec (assuming full ICU support).

              QString string = "...";
              QTextCodec *codec = QTextCodec::codecForName("CP437");
              QByteArray encodedString = codec->fromUnicode(string);
              p->write(encodedString.constData());
              

              Of course, not everything you can put in a QString has a Code Page 437 equivalent.

              Ramkumar MohanR 1 Reply Last reply
              3
              • Ramkumar MohanR Ramkumar Mohan has marked this topic as solved on
              • C ChrisW67

                @Ramkumar-Mohan said in doesn't print the ( µ ) symbol correctly in qt:

                To expand on @hskoglund's input.

                QString teststr3 = QString().fromLatin1("\xb5");
                This puts a 16-bit number containing 0x00b5 into the QString. This is UTF-16 form of the character you provided from the ISO-8859-1 (Latin1) set.

                p->write(" symbol : " + teststr3);
                This code will probably invoke this operator to produce a QString containing both parts.

                You do not say what the write() function is expecting so I am guessing it is a crude char*. The QString will be cast to char* using UTF-8 encoding by default. This generates two bytes for the last character in the QString:

                • 0xC2 -> Which is the ┬ character in the 8-bit Code Page 437
                • 0xB5 -> Which is the ╡character in the Code Page 437

                Those are the characters you see on the paper.
                To get a µ in the code page (on that printer) you need to send a 0xe6 byte.

                You can deliberately convert your QString to a 8-bit string in the correct encoding using QTextCodec (assuming full ICU support).

                QString string = "...";
                QTextCodec *codec = QTextCodec::codecForName("CP437");
                QByteArray encodedString = codec->fromUnicode(string);
                p->write(encodedString.constData());
                

                Of course, not everything you can put in a QString has a Code Page 437 equivalent.

                Ramkumar MohanR Offline
                Ramkumar MohanR Offline
                Ramkumar Mohan
                wrote on last edited by
                #7

                @ChrisW67

                Now , working fine ,

                 int num =230;
                 char data = char(num);
                 p->write(data);
                

                Thank you....

                1 Reply Last reply
                0

                • Login

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