Results 1 to 10 of 10

Thread: tr with #define..it can work?

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default tr with #define..it can work?

    Hello, im my default.h i put:
    Qt Code:
    1. #define aString "aString";
    To copy to clipboard, switch view to plain text mode 
    if i want to use aString as a QString in my project i used to do in this way
    Qt Code:
    1. ...
    2. QString ( aString )
    3. ...
    To copy to clipboard, switch view to plain text mode 
    but if i want internazionalizzate it with "tr()" function what should i do?
    i tried with tr
    Qt Code:
    1. tr ( QString ( aString ) )
    To copy to clipboard, switch view to plain text mode 
    but i get this error
    Qt Code:
    1. main.cpp:4396: error: no matching function for call to ‘main::tr(QString)’
    To copy to clipboard, switch view to plain text mode 

    thx

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: tr with #define..it can work?

    The tr signature is QObject::tr ( const char * sourceText, const char * comment = 0, int n = -1 ) .
    So you can just pass aString:
    Qt Code:
    1. QString translated = tr(aString);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tr with #define..it can work?

    good, now i don't get any error...but i did
    lupdate project.pro
    lrelease project.pro
    i can't find the voice in my .ts file...

  4. #4
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: tr with #define..it can work?

    From http://doc.trolltech.com/4.3/i18n.ht...l-literal-text:
    If you need to have translatable text completely outside a function, there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP(). They merely mark the text for extraction by the lupdate utility described below. The macros expand to just the text (without the context).

  5. The following user says thank you to bender86 for this useful post:

    mattia (4th February 2008)

  6. #5
    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: tr with #define..it can work?

    Why don't you do it this way ?
    Qt Code:
    1. const QString astring = QObject::tr("astring"); // global or wrapped in singleton class
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #6
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tr with #define..it can work?

    Quote Originally Posted by Gopala Krishna View Post
    Why don't you do it this way ?
    Qt Code:
    1. const QString astring = QObject::tr("astring"); // global or wrapped in singleton class
    To copy to clipboard, switch view to plain text mode 
    in this way i get a error
    Qt Code:
    1. error: ‘QString’ does not name a type
    To copy to clipboard, switch view to plain text mode 

  8. #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: tr with #define..it can work?

    Quote Originally Posted by mattia View Post
    in this way i get a error
    Qt Code:
    1. error: ‘QString’ does not name a type
    To copy to clipboard, switch view to plain text mode 
    Did you include <QString>?

    Btw, bender86 already pointed out the correct way to do it if you insist using defines.
    J-P Nurmi

  9. #8
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tr with #define..it can work?

    Hi, i did as bender86 suggest, i added in my default.h this:
    Qt Code:
    1. static const char *greeting_string = QT_TR_NOOP("Hellooooo");
    To copy to clipboard, switch view to plain text mode 
    and in my*ts file i translated greeting_string,
    Qt Code:
    1. <message>
    2. <location filename="../default.h" line="47"/>
    3. <source>Hellooooo</source>
    4. <translation>Ciaooooooooooooooo</translation>
    5. </message>
    To copy to clipboard, switch view to plain text mode 
    but when i call tr(greeting_string) in my application i'm not able to see greeting_string translated...I can just read "Hellooooo".
    With other QString in my application i I have not problem to see them translated.
    Any hint?
    thx

  10. #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: tr with #define..it can work?

    You must use QT_TRANSLATE_NOOP() outside class context. QT_TR_NOOP() is used inside class context.
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    mattia (4th February 2008)

  12. #10
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: tr with #define..it can work?

    thanks so much now it correctly works, but when i compile my application i keep having this annoying massage:
    Qt Code:
    1. default.h:48: warning: ‘greeting_string’ defined but not used
    To copy to clipboard, switch view to plain text mode 
    greeting_string is declared in this wy:
    Qt Code:
    1. static const char *greeting_string = QT_TRANSLATE_NOOP( "mainWindow" , "Hello" );
    To copy to clipboard, switch view to plain text mode 
    and it's used in mainWindow.
    thx

Similar Threads

  1. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  2. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20
  3. Macro used for debugging
    By sunil.thaha in forum General Programming
    Replies: 11
    Last Post: 30th March 2007, 17:32
  4. QtSingleApplication
    By mule in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2006, 19:21

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.