June 25, 2011

soroush soroush
Lab Rat
818 posts

Translating after QSetting is used to determine last state

 

Hi

I need to read a value from QSettings specifying the last UI language that user used:

  1. QApplication app(argc, argv);
  2. Configuration::createInstance();
  3. Configuration::Language lang =
  4.        (Configuration::Language)Configuration::settings->value("language").value<int>();
  5. if(lang == Configuration::Persian)
  6. {
  7.     QTranslator qtTranslation;
  8.     qApp->installTranslator(&qtTranslation);
  9. }

I got this warning in runtime
  1. KGlobal::locale::Warning your global KLocale is being recreated with a valid main component instead of a fake component, this usually means you tried to call i18n related functions before your main component was created. You should not do that since it most likely will not work
and translation is not done. but when remove QSettings and QTranslator became immediately after QApplication, everything works. I mean this:
  1.     QApplication app(argc, argv);
  2. //Configuration::Language lang =
  3. //      (Configuration::Language)Configuration::settings->value("language").value<int>();
  4. //if(lang == Configuration::Persian)
  5. //{
  6.     QTranslator qtTranslation;
  7.     qApp->installTranslator(&qtTranslation);
  8. //}

What’s happening here? Should translation be the first thing I do in program? why?

6 replies

June 26, 2011

Andre Andre
Robot Herder
6399 posts

I think the problem may be, that your Configuration class is static. At least, it looks that way from the code snippet you provide. That means that it is constructed, along with the QSettings I guess it contains, before the QApplication is constructed. Could you show what your Configuration class looks like?

June 26, 2011

soroush soroush
Lab Rat
818 posts

Configuration class:

  1. #ifndef CONFIGURATION_H
  2. #define CONFIGURATION_H
  3.  
  4. #include <QSettings>
  5.  
  6. class Configuration
  7. {
  8. public:
  9.     explicit Configuration();
  10.     static QSettings *settings;
  11.     static void createInstance();
  12.     enum Language{English,Persian};
  13. };
  14. // configuration.cpp:
  15. #include "configuration.h"
  16.  
  17. QSettings* Configuration::settings;
  18.  
  19. Configuration::Configuration()
  20. {
  21. }
  22.  
  23. void Configuration::createInstance()
  24. {
  25.     Configuration::settings = new QSettings("","program");
  26. }
  27.  
  28. #endif // CONFIGURATION_H

June 26, 2011

loladiro loladiro
Lab Rat
596 posts

Where are you constructing Configuration::settings?

EDIT: To clarify, I mean where are you calling createInstance to construct Configuration::settings.

June 26, 2011

soroush soroush
Lab Rat
818 posts

%1: Unknown error %2

After line #1 of first code. I edited my code :-P

June 26, 2011

loladiro loladiro
Lab Rat
596 posts

You could try to debug. From KGlobal.cpp

  1. 00173         // If you hit the warning below, here's how to debug it in gdb:
  2. 00174         // (gdb) set auto-solib-add on
  3. 00175         // (gdb) b qt_message_output
  4. 00176         // (gdb) run
  5. 00177         // It will stop at the "-nograb" information.
  6. 00178         // (gdb) b KLocalePrivate::KLocalePrivate
  7. 00179         // (gdb) c
  8. 00180         // And now it will stop at the first construction of the KLocale object, type bt or go up to find the
  9. 00181         // guilty i18n call.

June 26, 2011

soroush soroush
Lab Rat
818 posts

In the “Application Output” panel :

  1. &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"

I don’t know how to use debuggers. will take a look at documentation of gdb and come back.

 
  ‹‹ [Solved] 64bit osx      QMainwindow Qt::Applicationmodal ››

You must log in to post a reply. Not a member yet? Register here!