Qt can't get characters from Unicode string - qt

How can I get Unicode character (QChare type) from Unicode characters string (QString type).?I am trying with operator[] for Qstring object, and with it's member function at(), but it's not helping me(I'm using Qt Creator 2.0.1). I'm begginer in Qt, so this is maybe a simple question.

Did you try something like:
QString s("text");
QChar unicodeChar(s.at(0).unicode());

If you load your string into a QString, you can still use the .at(index) function. It will return a QChar which is a single wchar_t (UTF-16). You can cast that QChar to a wchar_t to get the unicode character.

Related

QRegExp and Null Character in Qt

i want search in a binary file with regular expression.
my search is successful in Text files, but not match in binary file, because QRegExp in function indexIn stop search when meet the NULL Character (chr(0)).
what can i do to solve this problem?
QString can contain null characters, it's just its constructors that are inconsistent...
QString::fromUtf8(const char *str, int size = -1) uses the given size, while QString::fromUtf8(const QByteArray &str) forces a strlen instead of using the bytearray size. See for yourself Qt code.
QRegExp also supports null characters:
QString s(QChar(0));
QRegExp re(s);
qDebug() << re.indexIn(s); // will print 0, not -1

Problems in converting to UTF-8 in Qt

I try to show a persian string in Qt:
QMessageBox msg;
QString str = "یا حسین";
msg.setText(QString::fromUtf8(str));
msg.exec();
but it shows the following error :
/home/msi/Desktop/VoMail
Project/Project/VoMail-build-desktop-Qt_4_8_1_in_PATH__System__Release/../VoMail/mainwindow.cpp:40:
error: no matching function for call to 'QString::fromUtf8(QString&)'
I want to use a string variable, and not a string directly.
How can I convert a QString variable to Utf8?
As seen here, QString::fromUtf8() does not accept an argument of type QString. You must give it a const char *, so you could rewrite it like this:
QMessageBox msg;
QString str = QString::fromUtf8("یا حسین");
msg.setText(str);
msg.exec();
its not good idea write like that
using this must be better
QString str(tr("ya hossein");
and use linguist and add persian translation file to your project http://qt-project.org/doc/qt-4.8/linguist-translators.html
and if you dont want use this, you must be sure your IDE or code editor (like qtcreator) use utf8 for saving files and just use
QString str("یا حسین");
it must be ok, i tested that so many times

what's the difference between QString and QLatin1String?

Like the title
1.what's the difference between QString and QLatin1String??
2.when and where do I need to use one of them??
3.following:
QString str;
str = "";
str = QLatin1String("");
Is "" == QLatin1String("")??
QString holds unicode. A string literal "foo" is a byte sequence that could contain text in any encoding. When assigning a string literal to a QString, QString str = "foo", you implicitely convert from a byte sequence in undefined encoding to a QString holding unicode. The QString(const char*) constructor assumes ASCII and will convert as if you typed QString str = QString::fromAscii("foo"). That would break if you use non-ascii literals in your source files (e.g., japanese string literals in UTF-8) or pass character data from a char* or QByteArray you read from elsewhere (a file, socket, etc.). Thus it's good practice to keep the unicode QString world and the byte array QByteArray/char* world separated and only convert between those two explicitly, clearly stating which encoding you want to use to convert between those two. One can define QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII to enforce explicit conversions (I would always enable them when writing a parser of any sort).
Now, to assign a latin1 string literal to a QString variable using explicit conversion, one can use
QString foo = QString::fromLatin1("föö");
or
QString foo = QLatin1String("föö");
Both state that the literal is encoded in latin1 and allow "encoding-safe" conversions to unicode.
I find QLatin1String nicer to read and the QLatin1String docs explain why it will be also faster in some situations.
Wrapping string literals, or in some cases QByteArray or char* variables, holding latin1 data for conversion is the main use for QLatin1String, one wouldn't use QLatin1String as method arguments, member variables or temporaries (all QString).
QString is Unicode based while QLatin1String is US-ASCII/Latin-1 based
Unicode is a super set of US-ASCII/Latin-1. If you only deal with US-ASCII/Latin-1 characters, the two are the same for you.
http://doc.qt.io/qt-4.8/qstring.html
http://doc.qt.io/qt-4.8/qlatin1string.html

How to convert wstring to LPOLESTR?

The below is the one that I have tried and it did not work.
std::wstring = L"Text";
USES_CONVERSION;
LPOLESTR lpDesc = W2OLE((LPWSTR)wsDescr.c_str());
Please any one cany say what is the better way to do?
LPOLESTR is a string of OLECHAR which is essentially wchar_t. So LPOLESTR is a null-terminated wchar_t*. LPOLESTR is a typedef created by Microsoft. These are vestiges of an automatic ANSI / Unicode conversion scheme that Microsoft used prior to MFC 4.0 and has since abandoned. For Win32 development, "OLE" corresponds to Unicode. For example, in Win32 development, an OLECHAR is simply a wchar_t and an LPOLESTR is a wide character string (e.g. wchar_t*).
To construct wstring from an array of wchar_t characters it is straight forward -
wchar_t* Array = L"Hello";
std::wstring strArray(Array);
to convert the other direction from wstring to wchar_t*, you can do this -
wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();
you can also try this,
LPOLESTR tempString = W2OLE((wchar_t*)wstring_temp.c_str());
You need no conversion at all, just copy the string:
std::wstring str = L"Text";
LPOLESTR lpDesc = (LPOLESTR) new wchar_t[str.length() + 1];
strcpy(lpDesc, str.c_str());
delete[] lpDesc;

Qt - Converting QString to Unicode QByteArray

I have a client-server application where client will be in Qt(Ubuntu) and server will be C#. Qt client willsend the strings in UTF-16 encoded format.
I have used the QTextCodec class to convert to UTF-16. But whenever the conversion happens it will be padded with some more characters. For example
"<bind endpoint='2_3'/>"
will be changed to
"\ff\fe<\0b\0i\0n\0d\0 \0e\0n\0d\0p\0o\0i\0n\0t\0=\0'\02\0_\03\0'\0/\0>\0\0\0"
I have following code which converts the QString to QByteArray
//'socketMessage' is the QString, listener is the QTcpSocket
QTextCodec *codec = QTextCodec::codecForName("UTF-16");
QByteArray data = codec->fromUnicode(socketMessage);
listener->write(data);
I have even tried the QTextStream,QDataStream etc for encoding. But everytime I end up with the same result. Am I doing something wrong here?
Though the question is asked long ago, I had the same problem. The solution to it is to create an QTextEncoder with the option QTextCodec::IgnoreHeader.
QTextCodec *codec = QTextCodec::codecForName("UTF-16");
QTextEncoder *encoderWithoutBom = codec->makeEncoder( QTextCodec::IgnoreHeader );
QString str("Foobar")
QByteArray bytes = encoderWithoutBom ->fromUnicode( s );
This will result in a QByteArray without BOM.
The \ff\fe at the beginning is the Unicode byte order mark (BOM) for UTF-16, little-endian. I'm not sure how to get the QTextCodec to ommit that but if you want to get a QByteArray from a string in UTF-16 without the BOM you could try this:
QString s("12345");
QByteArray b((const char*) (s.utf16()), s.size() * 2);

Resources