Results 1 to 20 of 28

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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

  3. #3
    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

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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

  8. #8
    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

  9. #9
    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

  10. #10
    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

  11. #11
    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

  12. #12
    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

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
  •  
Qt is a trademark of The Qt Company.