Trying to insert tr() returned string in Qhash
-
I am trying to createv a QHash <QString, QString> type and inserting the following key values
const QHash <QString, QString> ClassName:MyHash = { {("test"), tr("Test")}, {("test1"), tr("Test1")} }when launching the application its crashing when executing the above code.
-
I am trying to createv a QHash <QString, QString> type and inserting the following key values
const QHash <QString, QString> ClassName:MyHash = { {("test"), tr("Test")}, {("test1"), tr("Test1")} }when launching the application its crashing when executing the above code.
@Ankit1999 said in Trying to insert tr() returned string in Qhash:
application its crashing
How does stack trace look like?
-
@jsulm i am getting an access violation reading location .. in the call stack i see that it has entered the tr() method and crashed internally in some qt core dll
@Ankit1999 Please post the stack trace
-
@jsulm i am getting an access violation reading location .. in the call stack i see that it has entered the tr() method and crashed internally in some qt core dll
@Ankit1999 I think this is going to be a problem.
try
const char *instead of a tr marked string:const QHash <QString, const char *> ClassName:MyHashthan mark the string literal for translation:
const QHash <QString, const char *> ClassName:MyHash = { {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")}, {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")} }than send the string literal through the translation unit on read:
QCoreApplication::translate("MyHash", myHash.value("test")); -
I am trying to createv a QHash <QString, QString> type and inserting the following key values
const QHash <QString, QString> ClassName:MyHash = { {("test"), tr("Test")}, {("test1"), tr("Test1")} }when launching the application its crashing when executing the above code.
@Ankit1999 said in Trying to insert tr() returned string in Qhash:
ClassName:MyHash
This does not even compile so how should it crash?
Please post either the stack trace or a minimal, compilable example.
I would guess it's a global static which gets initialized before Q(Core)Application so tr() will throw an assert. Don't use global statics (esp. not for such stuff where it's obviouslynot needed at all)
-
@Ankit1999 I think this is going to be a problem.
try
const char *instead of a tr marked string:const QHash <QString, const char *> ClassName:MyHashthan mark the string literal for translation:
const QHash <QString, const char *> ClassName:MyHash = { {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")}, {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")} }than send the string literal through the translation unit on read:
QCoreApplication::translate("MyHash", myHash.value("test")); -
@J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.
@Ankit1999 said in Trying to insert tr() returned string in Qhash:
@J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.
?
It is near literally a working example. let me make it one:int main(int argc, char *argv[]) { QApplication app(argc,argv); const QHash <QString, const char *> myHash = { {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")}, {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")} }; qDebug() << QCoreApplication::translate("MyHash", myHash.value("test")); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1")); QTranslator t; qDebug() << "Loading translation" << t.load("bla.ts"); app.installTranslator(&t); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test")); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1")); return app.exec(); } -
@Ankit1999 said in Trying to insert tr() returned string in Qhash:
@J-Hilk how do i add the translated strings using QT_TRANSLATE_NOOP the translated strings are not present in the hash even when i add the strings in qt linguist.
?
It is near literally a working example. let me make it one:int main(int argc, char *argv[]) { QApplication app(argc,argv); const QHash <QString, const char *> myHash = { {("test"), QT_TRANSLATE_NOOP("MyHash", "Test")}, {("test1"), QT_TRANSLATE_NOOP("MyHash", "Test1")} }; qDebug() << QCoreApplication::translate("MyHash", myHash.value("test")); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1")); QTranslator t; qDebug() << "Loading translation" << t.load("bla.ts"); app.installTranslator(&t); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test")); qDebug() << QCoreApplication::translate("MyHash", myHash.value("test1")); return app.exec(); }