qt qstring toStdString loss of minus sign - qt

I am using a qstring and using the function toStdString(). When I do this I lose a minus sign:
'332-_09I_W'
this text becomes:
'332_09I_W'
What can I do to prevent this?
EDIT: Actually, the problem is not when i use toStdString(), it is when I set the text in my qTextEdit. The change occurs here:
myTextEdit->setHtml(myString);

I've tried:
QString qs("332-_091_W");
std::string st = qs.toStdString();
ui->textEdit->setHtml(st.c_str());
It gives no problem to me. Which version of Qt are you using?
However, from the documentation:
setHtml() changes the text of the text edit. Any previous text is removed
and the undo/redo history is cleared. The input text is
interpreted as rich text in html format.
Note: It is the responsibility of the caller to make sure that the text is correctly decoded when a QString containing HTML is created
and passed to setHtml().
The minus/hyphen symbol is ambiguous in HTML, try to change it in the QString (before passing it to setHtml()) with −

Related

How to get a superscript in QTableWidget header?

I found out from this question that If I want to have superscript in labels I can use this solution:
µm<sup>2</sup>
which means that I can use HTML tags and it works well.
but when I tried this way in QTableWidget, it didn't work.
This is its result:
I try other HTML tags which work in QLabel but none of them work for QTableWidgetItem
This is the solution for having superscript in the QTableWidget header :
We should use only Unicode characters for example:
QTableWidgetItem *___qtablewidgetitem = tableWidget->horizontalHeaderItem(0);
___qtablewidgetitem->setText(QCoreApplication::translate("MainWindow", "x\302\262", nullptr));
QTableWidgetItem *___qtablewidgetitem1 = tableWidget->horizontalHeaderItem(1);
___qtablewidgetitem1->setText(QCoreApplication::translate("MainWindow", "x\302\263", nullptr));
QTableWidgetItem *___qtablewidgetitem2 = tableWidget->horizontalHeaderItem(2);
___qtablewidgetitem2->setText(QCoreApplication::translate("MainWindow", "x\342\202\211", nullptr));
In the above example, I use "x\302\262" to see x².
This will be the result
Because it may be difficult to know these numbers, I used an online keyboard.
I wrote the content I wanted to see in the header here and after that I use Ctrl+C in Table's header UI.
then Qt will generate Unicode forms in "ui_mainwindow.h" file.
As I mentioned characters should be Unicode so for example I test typing my text in LibreOffice and Ctrl+C x² from there but it didn't understand and show x2 in the header.

How to display the content of an ELF file in QTextEdit?

I need to display all the bytes from and ELF file to a QTextEdit and i did not find any reasonable way to do this. I could print maximum "?ELF??" then nothing. The content of the ELF is read in a char* array (this is a requirement, can't change that) and yes, for sure the content is read.
I am guessing that your code looks something like this:
char *elf = ReadElfFile();
QString str(elf); // Constructs a string initialized with the 8-bit string str.
QTextEdit edit(str);
The problem is that QString constructor will stop on first NUL character, and the ELF file is full of them.
If you want to make a QString that contains NULs, do something like this:
QString str(QByteArray(elf, length_of_elf));
This just nearly broke me too, so I'll post my solution to anyone interested.
Let's say I have a QByteArray data that is filled like so
data += file.readAll();
I'll then invoke an update of the QTextEdit where I'll do
QByteArray copy = data;
QString text = copy.replace((char)0x00, "\\0");
textEdit.setPlainText(text);
This way, all null bytes in the data will be displayed as the printable string \0.
Since I want changes of the textEdit to be reflected in my data, I have to parse this back using
QByteArray hex = textEdit.toPlainText().toUtf8().toHex().toUpper();
hex.replace("5C30", "00");
hex.replace("5C00", "5C30"); // oops, was escaped
data = QByteArray::fromHex(hex);
I'm using the hex format because I just could not get the replace to work with null byte characters. The code above first replaces all occurrences of the string \0 with null bytes in the data. Then it replaces any \ followed by a null byte back with \0 - which essentially means \\0 becomes \0.
It's not very elegant, but maybe it helps anyone ending up here to move on in the right direction. If you have improvements, please comment.

How can I display unicode in QGraphicsTextItem? [duplicate]

This question already has answers here:
How to specify a unicode character using QString?
(4 answers)
Closed 7 years ago.
I would like to be able to display Unicode in QGraphicsTextItem (or a subclass of it).
The only way to set text in QGraphicsTextItem seems to be
setPlainText(text);
Trying
setPlainText(QString::fromUtf8("Caf\x00e9 Frap\x00e9"));
or
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Café Frapé");
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
setPlainText("Caf\x00e9 Frap\x00e9");
I get:
Caf? Frap?
It seems that no matter what I do (which I am not sure is correct) I do not get the output right...
Do QGraphicsTextItem support unicode ? Is maybe the setPlainText function at fault - but then what are the alternatives ? (I looked into setDocument but it also sets plain text...)
Edit - copying the special characters inside the QGraphicsTextItem works, once on screen, but still unable to place any unicode from code.
In a class inheriting QGraphicScene, I used:
QString text(QString::fromUtf8(xt.text));
...
QGraphicsTextItem *t = addText(text = text.replace("\\n", "\n"), font);
The dot source is utf8:
digraph so {
Café -> Frapé
}
And the rendering:
You can find here the C++ code.
I think you should use the
QGraphicsTextItem item.
item.setHtml( "Café Frapé" );
function instead of the mentioned. Read this QGraphicsTextItem::setHtml.

How to determine the encoding of the text in a QTextEdit in Qt?

I am obtaining the content from a QTextEdit object by using the following code:
QString text=my_QTextEdit.toPlainText();
What is the encoding that QTextEdit uses, a what encoding is used in the QString I get back from the toPlainText() call?
Thanks.
QTextEdit.toPlainText() returns a QString object, which is always a unicode character string (see documentation).
The QString class provides the functions toLatin1(), toAscii() and toUtf8(), which allow you to convert the string from unicode to an 8-bit string that you can process further. So Qt handles the encoding & decoding of the string for you.
If you want to create a QString instance from a given byte-string, you can use the functions fromAscii(), fromLatin1() or fromUtf8().
All controls in Qt are enabled for 16-bit characters. That means that content of a QTextEdit is Unicode (or UTF-32/UCS-4) (see also http://developer.nokia.com/Community/Discussion/showthread.php/215203-how-to-correctly-display-Unicodes-in-QPlainTextEdit).
When getting the content of a QTextEdit control (via plainText()), you get back a QString which contains Unicode.
From there on, you can convert to other format as you like: toUTF8(), toUCS4(), ...

Does QString::fromUtf8 automatically reverse a Hebrew string?

I am having a problem where a Hebrew string is being displayed in reverse. I use QTableWidget to display some info, and here the string appears correctly using:
CString hebrewStr; hebrewStr.ToUTF8();
QString s = QString::fromUtf8( hebrewStr );
In another part of my program this same string is displayed on the screen, but not using QT, and this is what is being shown in reverse:
CString hebrewStr;
hebrewStr.ToUTF8();
I have debugged and hebrewStr.ToUTF8() in both cases produces the exact same unicode string, but the string is only displayed correctly in the QTableWidget. So I am wondering if Qt automatically reverses a given Hebrew string (since it is a rigth-to-left language). Thanks!
Yes, in this case QString generate the full unicode wchar_t from the UTF-8 encoded string. If you would like to do similar thing in MFC, you should use CStringW and decode the string.
Use MultiByteToWideChar for UTF8 to CStringW conversion.
Connected question in StackOverflow.

Resources