When i try read non-latin characters (e. g. russian) from QSettings in QT i have wrong values, something like "Ð\224адад". How to do it? Please, help.
I use ubuntu.
Try first reading byte array and then convert it to string from UTF8, e.g.:
QSettings settings("filename.ini", QSettings::IniFormat);
QByteArray ba = settings.value("goup/key").toByteArray();
QString str = QString::fromUtf8(ba.data(), ba.length());
Related
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.
I am trying to read percentage encoded urls with umlauts, such as äüö,..., with Qt:
QString str = "Nu%CC%88rnberg"
qDebug() << QUrl::fromPercentEncoding(str.toUtf8());
But the output is Nu¨rnberg instead of Nürnberg. How can I correctly decode urls with umlauts in this form?
Regards,
I have done this issue but I am little confused with result. First if you want to use letter ü use %C3%BC not %CC%88 (according to https://www.w3schools.com/tags/ref_urlencode.asp). So you need
QString str = "N%C3%BCrnberg";
QString encoded = QUrl::fromPercentEncoding(str.toUtf8());
But if you output it in qDebug() stream you can get different symbol (I guess it is because your default system encoding). But if you output it in GUI element you will have your ü symbol
QMessageBox::information(this, "", encoded);
this means main window.
I'm receiving a binary thru FTP and storing it as a QString. The original image was BMP but to pass through FTP I converted it to QPixmap. I'm able to successfully receive this binary but I can't convert back to a QPixmap or some sort of format to display that image.
Does anyone have any suggestion?
1) Convert QString to QByteArray data with QString::toLatin1(), QString::toLocal8Bit(). Think work only one of methods
2) Read QByteArray with
bool QPixmap::loadFromData(const QByteArray & data, ...
Possibly functions in point one will encode data to another(text) encoding. Then try to convert QChar* from QString::data to QByteArray. Or write that QChar's into file and read QPixmap from file.
I don't understand what happens when I create a text stream and then do setCodec("some_encoding"), does it start assuming that the file I'm reading from is in some_encoding and when I do QTextStream::readAll return me a QString in some_encoding? Or does QTextStream::readAll return a QString in unicode?
Here's what I do:
QString read(const char* encoding)
{
QTextStream stream(&file);
stream.setCodec(encoding);
return stream.readAll();
}
But I don't get a unicode string back. So, bottom line is, I want to know, how, having a file in some encoding, do I save all the contents as Unicode into a QString? If readAll() returns a string in the encoding specified, how do I convert that QString from that encoding to unicode?
Turns out this didn't have anything to do with encodings. I did stream.seek(0) before reading and it read it all right. I suspected that the problem was with encodings because usually when they're off you either get questions marks or empty strings everywhere, in this case I got an empty string.
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(), ...