doesn't print the ( µ ) symbol correctly in qt
-
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:-
how to fit it....
-
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);
-
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);
@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; }
-
@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; }
@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. -
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 trychar 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
-
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:-
how to fit it....
@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.
-
R Ramkumar Mohan has marked this topic as solved on
-
@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.