Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: QSettings with translation

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSettings with translation

    Hi to all,
    I would know how set the settings of my application to manage translation.
    For example if I choose english, when I restart the application I would have english as language.

    Best Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    What have you already done to try and solve your problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by wysota View Post
    What have you already done to try and solve your problem?
    pass a file path, set a QString, but I would know if there is a specific setting
    Franco Amato

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    So does it work or not? If not, what doesn't work?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by wysota View Post
    So does it work or not? If not, what doesn't work?
    Doesn't work! If I change language it always load the same.
    So I ask again: is there a specific setting for language translation?
    Franco Amato

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    So how exactly do you store the setting?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by wysota View Post
    So how exactly do you store the setting?
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *ce )
    2. {
    3. Q_UNUSED(ce);
    4. writeSettings();
    5. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void MainWindow::writeSettings()
    2. {
    3. QSettings settings("config.ini", QSettings::IniFormat);
    4. /* language group */
    5. settings.beginGroup("language");
    6. settings.setValue("lang", m_language);
    7. settings.endGroup();
    8. /* paths group */
    9. settings.beginGroup("paths");
    10. settings.setValue("w1_source", m_w1SourceDir);
    11. settings.setValue("w2_source", m_w2SourceDir);
    12. settings.setValue("w1_output", m_w1OutputDir);
    13. settings.setValue("w2_output", m_w2OutputDir);
    14. settings.endGroup();
    15. /* colors group */
    16. settings.beginGroup("colors");
    17. settings.setValue("background", m_bgColor);
    18. settings.setValue("waveform", m_wfColor);
    19. settings.setValue("timeline", m_tlColor);
    20. settings.setValue("markers" , m_mkColor);
    21. settings.setValue("selection", m_slColor);
    22. settings.endGroup();
    23. /* background */
    24. settings.beginGroup("background");
    25. settings.setValue("file", m_bgFile);
    26. settings.setValue("loop_interval", m_loopInterval);
    27. settings.setValue("loop_mode", m_loopMode);
    28. settings.endGroup();
    29. /* steps */
    30. settings.beginGroup("steps");
    31. settings.setValue("sec", m_sstep);
    32. settings.setValue("min", m_mstep);
    33. settings.endGroup();
    34. }
    To copy to clipboard, switch view to plain text mode 
    Franco Amato

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    I'm not sure that storing the settings in the current working directory is a good idea. Anyway, how does the reading part look like? Also does the configuration file get created? Does it have proper contents?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by wysota View Post
    I'm not sure that storing the settings in the current working directory is a good idea.
    So how must I do?
    Anyway, how does the reading part look like?
    It works for all settings except for the language

    Also does the configuration file get created? Does it have proper contents?
    Yes the content of the ini file is correct
    Franco Amato

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    Quote Originally Posted by franco.amato View Post
    So how must I do?
    Either store the settings in a "well known location" like the user's home directory or use the application binary directory or let Qt choose the proper location.

    It works for all settings except for the language
    Please provide enough information for us to see how you try to apply the language change using your settings. I'm tired of having to drag every piece of information out of you. If you want help, provide enough information to see where the problem might be. And do it always, not only in this thread, you have already over 400 posts on this forum, for God's sake! Every thread of yours is at least 5 times longer than questions asked by others. If you have problem with specifying your problem, maybe you should take a look at how others do it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings with translation

    and you read your settings file how?

  12. #12
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by wysota View Post
    Either store the settings in a "well known location" like the user's home directory or use the application binary directory or let Qt choose the proper location.
    How can I let Qt decide where? Can I speak with Qt and ask where it would store the configuration file?


    Please provide enough information for us to see how you try to apply the language change using your settings. I'm tired of having to drag every piece of information out of you. If you want help, provide enough information to see where the problem might be. And do it always, not only in this thread, you have already over 400 posts on this forum, for God's sake! Every thread of yours is at least 5 times longer than questions asked by others. If you have problem with specifying your problem, maybe you should take a look at how others do it.
    Please wysota give a look at your replies. They are also written with Morse code, and I always have to ask 'please can you be more clear' or I always have to beg for 2 lines of clarifing code of example as in the above statement ( let Qt decide where ).
    I'm sure I'm not the only person thinking that.
    I stopped to post questions in the 'Programming' sections hoping to have more clear replies (to improve my Qt level) but is not so.
    Franco Amato

  13. #13
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by squidge View Post
    and you read your settings file how?
    This is my routine to read settings:

    Qt Code:
    1. void MainWindow::readSettings()
    2. {
    3. QSettings settings("config.ini", QSettings::IniFormat);
    4. /* language group */
    5. settings.beginGroup("language");
    6. m_language = settings.value("lang", "en").toString();
    7. settings.endGroup();
    8. /* paths group */
    9. settings.beginGroup("paths");
    10. m_w1SourceDir = settings.value("w1_source", "C:").toString();
    11. m_w2SourceDir = settings.value("w2_source", "C:").toString();
    12. m_w1OutputDir = settings.value("w1_output", "C:").toString();
    13. m_w2OutputDir = settings.value("w2_output", "C:").toString();
    14. settings.endGroup();
    15. /* background file */
    16. settings.beginGroup("background");
    17. m_bgFile = settings.value("file", "./audio de fondo/Grabacion_marca de agua_cruz report - MONO.mp3").toString();
    18. m_loopMode = settings.value("loop_mode", "loop").toString();
    19. m_loopInterval = settings.value("loop_interval", 5).toInt();
    20. settings.endGroup();
    21. /* steps */
    22. settings.beginGroup("steps");
    23. m_sstep = settings.value("sec", 1).toInt();
    24. m_mstep = settings.value("min", 1).toInt();
    25. settings.endGroup();
    26. /* colors group */
    27. settings.beginGroup("colors");
    28. QColor defColor = Qt::white;
    29. QVariant colVariant = defColor;
    30. m_bgColor = settings.value( "background", colVariant ).value<QColor>();
    31. m_wfColor = settings.value( "waveform", Qt::darkBlue ).value<QColor>();
    32. m_tlColor = settings.value( "timeline", Qt::darkRed ).value<QColor>();
    33. m_mkColor = settings.value( "markers", Qt::yellow ).value<QColor>();
    34. m_slColor = settings.value( "selection", Qt::red ).value<QColor>();
    35. settings.endGroup();
    36. }
    To copy to clipboard, switch view to plain text mode 

    I installed 2 translation files in the main.cpp so:

    Qt Code:
    1. QApplication app(argc, argv);
    2.  
    3. QTranslator enTranslator;
    4. enTranslator.load( "ecpstudio_en");
    5. app.installTranslator( &enTranslator );
    6.  
    7. QTranslator esTranslator;
    8. esTranslator.load( "ecpstudio_es");
    9. app.installTranslator( &esTranslator );
    To copy to clipboard, switch view to plain text mode 

    then I perform a test on the m_language variable ( after read the configuration file )

    Qt Code:
    1. if ( QString::compare(m_language, "en", Qt::CaseInsensitive) == 0 )
    2. // here I would do effective the english translation file
    3. else
    4. // here I would do effective the spanish translation file
    To copy to clipboard, switch view to plain text mode 
    Last edited by franco.amato; 21st October 2010 at 23:53.
    Franco Amato

  14. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSettings with translation

    Why multiple installTranslator? Just call it once depending on which language you want and use QObject::tr() for translations.

  15. #15
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by squidge View Post
    Why multiple installTranslator? Just call it once depending on which language you want and use QObject::tr() for translations.
    So I only load them and then I'll install one or the other depending on the value of the variable?
    Can I do it in every point of the code?
    Last edited by franco.amato; 22nd October 2010 at 00:09.
    Franco Amato

  16. #16
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSettings with translation

    Quote Originally Posted by franco.amato View Post
    So I only load them and then I'll install one or the other depending on the value of the variable?
    Can I do it in every point of the code?
    Please - for once - go and read the documentation provided with Qt on internationalization. Qt's system isn't the best I've seen, but it works and it's relatively simple. It does, however, require the person using it to make the small effort required to learn how it is used. There are extensive tutorials, documentation and sample code that describe all of the small number of details it possesses.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    Quote Originally Posted by franco.amato View Post
    How can I let Qt decide where?
    If you pass a file path using a constructor then the logical assumption would be to look at other constructors.

    Can I speak with Qt and ask where it would store the configuration file?
    If you want to "ask Qt" about something, you can read the reference manual.

    Please wysota give a look at your replies. They are also written with Morse code, and I always have to ask 'please can you be more clear' or I always have to beg for 2 lines of clarifing code of example as in the above statement ( let Qt decide where ).
    I'm expecting you to posses some knowledge. I won't tell you how to turn on your computer or login to your account. I expect you to know that. I also expect you to know how to do some basic things in Qt and if you are lazy enough not to find information about it yourself then I expect you to ask specific questions about problems you have. I expect to be talking not to a brainless brick but to an intelligent human being.

    I stopped to post questions in the 'Programming' sections hoping to have more clear replies (to improve my Qt level) but is not so.
    You won't improve yourself by asking questions. Learn by doing and trying to solve your problems first before you go somewhere and ask for help.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings with translation

    Quote Originally Posted by SixDegrees View Post
    Please - for once - go and read the documentation provided with Qt on internationalization. Qt's system isn't the best I've seen, but it works and it's relatively simple. It does, however, require the person using it to make the small effort required to learn how it is used. There are extensive tutorials, documentation and sample code that describe all of the small number of details it possesses.
    I already did.

    I modified my code removing the QTranslation instance from main.cpp and adding it to my MainWindow.cpp ctor so:

    Qt Code:
    1. MainWindow::MainWindow( QWidget *parent, Qt::WindowFlags flags )
    2. : QMainWindow(parent, flags),
    3. m_track1(""),
    4. m_track2(""),
    5. m_w1SourceDir(""),
    6. m_w2SourceDir(""),
    7. m_w1OutputDir(""),
    8. m_w2OutputDir(""),
    9. m_fileOneLoaded(false),
    10. m_fileTwoLoaded(false),
    11. m_trackListDlg(NULL)
    12. {
    13. setObjectName("MainWindow");
    14.  
    15. /* set window title */
    16. setWindowTitle("ECP Studio");
    17. //setMinimumSize(800, 400);
    18.  
    19. /* creo il menu e la status bar */
    20. createActions();
    21. createMenus();
    22. createToolBar();
    23. createStatusBar();
    24.  
    25. readSettings(); // here I fill the m_language variable
    26.  
    27. if( QString::compare(m_language,"es") == 0 )
    28. {
    29. QTranslator esTranslator;
    30. esTranslator.load("ecpstudio_es");
    31. QCoreApplication::instance()->installTranslator(&esTranslator);
    32. }
    33.  
    34. /* set the central widget */
    35. m_centralWidget = new CentralWidget( m_bgColor, m_wfColor, m_w1OutputDir, m_w2OutputDir, m_bgFile, m_mstep, m_sstep, m_loopInterval, this );
    36. setCentralWidget( m_centralWidget );
    37.  
    38. enableGlobalCommands( false );
    39.  
    40. connect( check, SIGNAL( stateChanged( int ) ), this, SLOT( uniqueFileChanged( int ) ) );
    41. connect( this, SIGNAL( synchSignal() ), m_centralWidget, SLOT( synchSlot()));
    42. connect( this, SIGNAL( globalPlaySignal() ), m_centralWidget, SLOT( globalPlaySlot()));
    43. connect( this, SIGNAL( globalStopSignal() ), m_centralWidget, SLOT( globalStopSlot()));
    44. connect( this, SIGNAL( globalASecondiSignal() ), m_centralWidget, SLOT( globalASecondiSlot()) );
    45. connect( this, SIGNAL( globalAMinutiSignal() ), m_centralWidget, SLOT( globalAMinutiSlot() ) );
    46. connect( this, SIGNAL( globalIMinutiSignal() ), m_centralWidget, SLOT( globalIMinutiSlot()) );
    47. connect( this, SIGNAL( globalISecondiSignal() ), m_centralWidget, SLOT( globalISecondiSlot()) );
    48. }
    To copy to clipboard, switch view to plain text mode 

    and it has no effect, language is always english ( instead of spanish as it should be - because I set 'es' in the configuration file ).

    Regards
    Last edited by franco.amato; 22nd October 2010 at 01:03.
    Franco Amato

  19. #19
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSettings with translation

    After 18 posts we have the full source code to answer your question. Great! But still we are missing some debug informations:
    • Is the if block entered?
    • Is load really returns true?
    And you might want to move the translator installation to the top, since you don't support dynamic language switching. So all actions and menus wont get retranslated. But that all stands in the documentation as well.

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings with translation

    Franco, tell me, what is the value of "x" in the place marked with /* ??? */ in the following code? Could you explain why? As a hint - you can write a little program to test it:

    Qt Code:
    1. if(true){
    2. int x = 7;
    3. }
    4. /* ??? */
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. UI translation
    By Annihilator in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2010, 12:37
  2. translation
    By weixj2003ld in forum Qt Programming
    Replies: 8
    Last Post: 31st May 2010, 02:01
  3. Translation of UI
    By elizabeth.h1 in forum Qt Programming
    Replies: 1
    Last Post: 2nd November 2009, 12:01
  4. Migrate Qt3 QSettings to Qt4 QSettings
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 22nd February 2008, 04:21
  5. Translation
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2007, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.