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
Related
I have custom(dynamic QString) for example something like this 123+555 and i need to get this after +.Also there can be something different then + (/,*,- etc.). My question is how to get part of string after some char.
Use the split function, which allows you to specify the separator and returns a list of the elements.
QString string("123+555");
QStringList listItems = string.split('+', QString::SkipEmptyParts);
QString finalString = listItems[1];
Alternatively, you can find by index the separating character location and use that with a call to right
Since you're usin Qt, you could try the class: QRegExp.
With such class you could write code like this:
// This code was not tested.
QRegExp rx("(\\d+)(\\+|\\-|\\*|/)(\\d+)"); // Be aware, I recommend you to read the link above in order to see how construct the proper regular expression.
int pos = rx.indexIn("23+344");
if (pos > -1) {
QString number_1 = rx.cap(1); // "23"
QString op = rx.cap(2); // "+"
QString number_2 = rx.cap(3); // "344"
// ...
}
This way you don't have to write code to check which of the characters(operators) "+, -, *, /" is present to then perform a split on the string depending on what character was found.
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.
QDir *temp = new QDir("%USERPROFILE%");
bool ok = temp->mkdir("abc");
and it does not work.
For portable Qt code, use static method QProcessEnvironment::systemEnvironment():
QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
QDir *temp = new QDir(env.value("USERPROFILE")); // returns empty string for unset variable
bool ok = temp->mkdir("abc");
Alternative, as suggested in a comment of the actual question, you can also use
#include <cstdio>
QString envValue(QString::fromLocal8bit(::getenv("USERPROFILE"))); // standard, yet deprecated in Windows...
I'd prefer using the first alternative, letting Qt take care of character encoding stuff. If you want to use non-Qt functions, I think in Windows you should use getenv_s or _wgetenv_s to do this "properly".
I've tried to convert using the following code:
template< unsigned int size >
static QString
TBuf82QString( const TBuf8< size > &buf )
{
return QString::fromUtf16(
reinterpret_cast<unsigned short*>(
const_cast<TUint8*>(
buf.Ptr() ) ), buf.Length() );
}
But It always returns something like ?????b.
EDIT: Changed code example
Using a template probably isn't a good solution, since it will result in a new instantiation of this block of code within your application binary, for every size of input string which is converted. Since the output type (QString) contains no compile-time constant, this means you end up with code bloat, for no gain.
A better approach would be to leverage the fact that TBuf8<N> inherits from TDesC8:
QString TBuf2QString(const TDesC8 &buf)
{
return QString::fromLocal8Bit(reinterpret_cast<const char *>(buf.Ptr()),
buf.Length());
}
TBuf<16> foo(_L("sometext"));
QString bar = TBuf2QString(foo);
TBuf8 is used for binary data or non-Unicode strings. TBuf16 is used for Unicode strings. TBuf is conditionally compiled and will always be TBuf16 as Symbian OS is natively Unicode.
Try using QString::fromLocal8Bit() with TBuf8::Ptr()
The following code somehow fails to notice any files with non-ASCII characters in their names (Cyrillic characters, specifically):
for (int path = 1; path < argc; path++) {
QFileInfo fi(argv[path]);
if (fi.isDir()) {
QDir dir(argv[path], "", QDir::LocaleAware, QDir::AllEntries);
qDebug() << dir.entryList();
QDirIterator it(QString(argv[path]), QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
qDebug() << it.fileInfo().absoluteFilePath();
/* Processing; irrelevant in the context of the question */
}
}
}
What exactly am I doing wrong here? How should I handle QDir and QDirIterator to make them aware of Cyrillic filenames?
The system locale is en_US.UTF-8.
Update: On Windows, everything works correctly.
Get cmd line parameters out of QApplication itself.
So
QApplication app(argc, argv);
QStringList args = app.arguments();
for(...)
Qt will handle encoding properly. But that will only fix problems with unicode on cmd line. Not sure if that is your main problem though.
EDIT:
fromLocal8Bit() probably doesn't work because it wasn't local encoding, but utf8. So fromUtf8() would work on linux and osx (but it won't work on windows). On *nuxes it depends on some environment variables (LS_LANG or something). I guess Qt takes everything into account and converts it properly. You can look at the constructor code for QApplication if you want to know exactly what they do.
Which part is failing? Reading the initial directory specified argv[path] or the iterator? If it's the former, you should convert byte strings to QString for file processing using QFile::decodeName. The default char* => QString conversion uses Latin-1, which is not what you want for file names.
Don't use argv[path] just like that when constructing the QStrings. This will treat the string as a latin1 string (which doesn't care about cyrillic characters). Try using
const QString dirName = QString::fromLocal8Bit( argv[path] );
at the top of your loop and then use dirName everywhere instead of argv[path].