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

Thread: Dynamic translation (was: How to get QApplication pointer)

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Dynamic translation (was: How to get QApplication pointer)

    Hi to all!

    I have an QApplication object in which QTranslator is installed. It works perferctly. But now I have a problem. I have a "grandchild" qwidget that holds language selection QPushButton subclassed objects. How do I launch QApplication::loadTranslator from this window??
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to get QApplication pointer

    Quote Originally Posted by MarkoSan View Post
    Hi to all!

    I have an QApplication object in which QTranslator is installed. It works perferctly. But now I have a problem. I have a "grandchild" qwidget that holds language selection QPushButton subclassed objects. How do I launch QApplication::loadTranslator from this window??
    Use qApp
    qApp->loadTranslator();
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    Hmm, this does not work:
    Qt Code:
    1. void CLanguageSelectorWidget::translateSlovene()
    2. {
    3. m_Translator.load("translations/eROSystem_client_SL"); // loads translator
    4. //QApplication::installTranslator(&m_Translator); // installs it
    5. qApp->installTranslator(&m_Translator); // installs it
    6. emit accept(); // closes window
    7. }
    To copy to clipboard, switch view to plain text mode 

    Why not???

    In main.cpp there is a very similar code that works:
    Qt Code:
    1. // translator setup
    2. QTranslator qtTranslator;
    3. qtTranslator.load("translations/eROSystem_client_SL");
    4. a.installTranslator(&qtTranslator);
    To copy to clipboard, switch view to plain text mode 

    Why the upper code chunk does not work?? Signals/Slots are ok, I've checked.
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    In main() the QTranslator object remains alive because QApplication::exec() blocks until the application quits. Is CLanguageSelectorWidget allocated on the stack? The translator object goes out of scope as soon as CLanguageSelectorWidget is destructed.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    How come, if m_Translator is defined in its header file:
    Qt Code:
    1. #ifndef CLANGUAGESELECTORWIDGET_H_
    2. #define CLANGUAGESELECTORWIDGET_H_
    3.  
    4. /*!
    5.  * \class CLanguageSelectorWidget
    6.  * \author VSistemi Marko Frelih s.p.
    7.  * \version 1.0
    8.  * \date January 2007
    9.  * \brief This Qt based class represents a language selector widget, which constist of number of
    10.  * CFlagButton objects.
    11.  * \details version 1.00 (revision 84): basic functionality
    12. */
    13.  
    14. // qt includes
    15. #include <QDialog>
    16. #include <QHBoxLayout>
    17. #include <QVBoxLayout>
    18. #include <QList>
    19. #include <QFont>
    20. #include <QPalette>
    21. #include <QTranslator>
    22. #include <QApplication>
    23.  
    24. class QWidget; // forward declaration
    25.  
    26. // custom includes
    27. #include "CFlagButton.h"
    28.  
    29. class CLanguageSelectorWidget : public QDialog
    30. {
    31. Q_OBJECT
    32.  
    33. public:
    34. CLanguageSelectorWidget(QWidget* pParent);
    35. ~CLanguageSelectorWidget();
    36. inline QPointer<QHBoxLayout> langaugeButtonsLayout() { return m_pLanguageButtonsLayout; };
    37. inline QPointer<QVBoxLayout> mainLayout() { return m_pMainLayout; };
    38. //inline QList<CFlagButton> languageButtonsList() { return mLanguageButtonsList; };
    39. inline QPalette widgetPalette() { return m_WidgetPalette; };
    40. inline QPointer<QPushButton> sloLangButton() { return m_pSloLangButton; };
    41. inline QPointer<QPushButton> ukLangButton() { return m_pUKLangButton; };
    42. inline QPointer<QPushButton> deuLangButton() { return m_pDeuLangButton; };
    43. inline QPointer<QPushButton> itaLangButton() { return m_pItaLangButton; };
    44.  
    45. public:
    46. // TODO: this member should be private
    47. //QList<CFlagButton> mLanguageButtonsList; // language buttons list
    48.  
    49. private:
    50. QPointer<QHBoxLayout> m_pLanguageButtonsLayout; // horizontal layout
    51. QPointer<QVBoxLayout> m_pMainLayout; // main layouts
    52. QPointer<QPushButton> m_pSloLangButton; // slo lang
    53. QPointer<QPushButton> m_pUKLangButton; // eng lang
    54. QPointer<QPushButton> m_pDeuLangButton; // deu lang
    55. QPointer<QPushButton> m_pItaLangButton; // ita lang
    56. QPalette m_WidgetPalette; // widget palette
    57. QTranslator m_Translator; // application translator
    58.  
    59. private slots:
    60. void translateSlovene(); // launches slovenian translation
    61. void translateEnglish(); // launches english translation
    62. void translateDeutsch(); // launches german translation
    63. void translateItaliano(); // launches italian translation
    64. };
    65.  
    66. #endif /*CLANGUAGESELECTORWIDGET_H_*/
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Where do you show/exec CLanguageSelectorWidget? Consider following code:
    Qt Code:
    1. {
    2. CLanguageSelectorWidget dialog(this);
    3. if (dialog.exec() == QDialog::Accepted)
    4. {
    5. }
    6. } // the translator gets destructed here together with CLanguageSelectorWidget
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    So, what do you suggest, how should I recode?
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Allocate the translator on the heap. In other words, create the translator with keyword "new". It must remain alive after the dialog gets destructed, right?
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    Yes, you are right. God damn, I've forgot the basics ... I've allocated it, but same result. I think object gets destructed again after leaving QDialog:
    Qt Code:
    1. m_pTranslator=new QTranslator(this); // creats new translator
    2. Q_CHECK_PTR(m_pTranslator); // checks creation
    3. m_pTranslator->load("translations/eROSystem_client_ITA"); // loads translator
    4. //QApplication::installTranslator(&m_Translator); // installs it
    5. qApp->installTranslator(m_pTranslator); // installs it
    6. emit accept(); // closes window
    To copy to clipboard, switch view to plain text mode 

    Am I right?
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Don't pass "this" as its parent. Every QObject will automatically delete its children. Here, the dialog will delete the translator. Try passing for example "qApp" as parent instead.
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    I did, still no result. Is it possible the path to translator file is corrupted??? Is it ok to use "/" in path under windows instead of "\'
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Quote Originally Posted by MarkoSan View Post
    I did, still no result. Is it possible the path to translator file is corrupted???
    QTranslator::load() returns true if the translation is successfully loaded; otherwise returns false. So check the return value!

    Is it ok to use "/" in path under windows instead of "\'
    Yes, it's ok. Qt will translate your paths to conform to the underlying operating system.
    J-P Nurmi

  13. #13
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Red face Re: How to get QApplication pointer

    Here is the code:
    Qt Code:
    1. m_pTranslator=new QTranslator(qApp); // creats new translator
    2. Q_CHECK_PTR(m_pTranslator); // checks creation
    3. if(m_pTranslator->load("translations/eROSystem_client_ITA")) // loads translator
    4. {
    5. //QApplication::installTranslator(&m_Translator); // installs it
    6. qApp->installTranslator(m_pTranslator); // installs it
    7. emit accept(); // closes window, success
    8. } else {
    9. emit reject(); // failure
    10. }
    To copy to clipboard, switch view to plain text mode 
    Every language button succesfully loads translator. But still no result!!!
    Qt 5.3 Opensource & Creator 3.1.2

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Quote Originally Posted by MarkoSan View Post
    Every language button succesfully loads translator. But still no result!
    What does this mean? Do your widgets catch QEvent::LanguageChange?

    PS. QDialog::accept() and QDialog::reject() are not signals so drop those "emits" for sake of clarity.
    J-P Nurmi

  15. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Oh, and for dynamic translation you might want to check this wiki article out: [WIKI]Dynamic translation[/WIKI]
    J-P Nurmi

  16. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    I've read the wiki, but what is the point of translating application using QLinguist if I must enter tr() texts MANUALY in changeEvent. And how does application know which language was changed to???
    Qt 5.3 Opensource & Creator 3.1.2

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    Quote Originally Posted by MarkoSan View Post
    I've read the wiki, but what is the point of translating application using QLinguist if I must enter tr() texts MANUALY in changeEvent.
    You might want to read it again, you must have missed something. If you mean text written in Designer, UIC provides a retranslateUi() method which is mentioned in the article.

    And how does application know which language was changed to???
    QObject::tr() and QApplication::translate() work based on installed translators.
    J-P Nurmi

  18. #18
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    Quote Originally Posted by jpn View Post
    You might want to read it again, you must have missed something. If you mean text written in Designer, UIC provides a retranslateUi() method which is mentioned in the article.


    QObject::tr() and QApplication::translate() work based on installed translators.
    I do not use Designer, all my classes are handcoded.
    Qt 5.3 Opensource & Creator 3.1.2

  19. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to get QApplication pointer

    So what's the problem? Everything in your code marked for translation (QObject::tr(), QApplication::tr(), etc.) is shown in Linguist. Let's say you write strings in code in English. You already have English version of your app without providing any translations. In Linguist you write translations to other languages.

    Edit: Oh, I think I just understood what you meant. No, you don't pass translated version to QObject::tr() and co. The whole point is to keep it for example English only in code and with help of tr() you get automatically provided the translation from installed translators. You just have to reset translatable strings when the language changes.
    J-P Nurmi

  20. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get QApplication pointer

    But I have Slovenian version in code. Then I've made 4 .ts files:
    - copy of Slovenian translation
    - English
    - Deutsch
    - Italiano

    Now, I smply do not know what to put in retranslate() function. All other things are getting clear now.
    Qt 5.3 Opensource & Creator 3.1.2

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.