Results 1 to 3 of 3

Thread: How to get foreign text in title bars, buttons, tool tips, etc.

  1. #1
    Join Date
    May 2014
    Posts
    9
    Thanks
    1

    Default How to get foreign text in title bars, buttons, tool tips, etc.

    I'm working on the tutorial at:
    http://zetcode.com/gui/pyqt4/firstprograms/
    It has been very helpful. I have been experimenting to expand my knowledge.
    In the first example program I see:
    # -*- coding: utf-8 -*-
    I assume that this means that the encoding is UTF-8. (No?)
    If I understand correctly, this means that I should be able to use other
    natural languages such as Russian, at least in strings, UI, etc. (No?)
    The title bar text is set by the line:
    w.setWindowTitle('Simple')
    I changed the string 'Simple" to Russian 'Простой', which is Russian for 'Simple'.
    When I execute the file the text is not properly displayed in the title bar.
    Similarly, when I put Russian text in the Tooltip example it is also not
    displayed properly.
    Similarly, when I change the label of the "Quit" button in that example the
    text is also not properly displayed.
    Is there something I have to do differently to get these things to work?
    Or are these labels dependent upon the system language (which in my
    case is English)? Or does this represent a limitation of the PyQt?
    Thanks in advance for any pointers.
    Tom

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get foreign text in title bars, buttons, tool tips, etc.

    In the first example program I see:
    # -*- coding: utf-8 -*-
    I assume that this means that the encoding is UTF-8. (No?)
    No, it means that the Python interpreter should assume the file is UTF-8 encoded and not strip out non-ASCII. You must ensure that the file is saved that way. If you are editing the file with emacs that same magic line will set the editor to the relevant mode. If you edit it with something else then you must ensure that is loads/saves text UTF-8 encoded and not, for example, using Latin 1 or a Windows-1252 (which will mangle the content).

    Then you must tell Python to create Unicode strings from the string literals:
    Qt Code:
    1. # These options all work for me
    2. w.setWindowTitle(u'Простой') # note the preceding 'u'
    3. w.setWindowTitle('Простой'.decode('utf-8'))
    4. w.setWindowTitle(unicode('Простой', 'utf-8'))
    To copy to clipboard, switch view to plain text mode 
    and PyQt will do the correct thing with them.

    Generally these fixed strings would be handled using Qt's translation mechanisms.

  3. The following user says thank you to ChrisW67 for this useful post:

    tom.hedden (8th May 2014)

  4. #3
    Join Date
    May 2014
    Posts
    9
    Thanks
    1

    Default Re: How to get foreign text in title bars, buttons, tool tips, etc.

    Hello ChrisW67,
    Thank you for your explanation.
    The Russian was/is definitely UTF-8 encoded: I'm very aware of that pitfall.
    I tried all three options that you suggested, and they all work fine.
    Thanks again,
    Tom

Similar Threads

  1. Replies: 6
    Last Post: 6th February 2013, 07:28
  2. Replies: 1
    Last Post: 4th December 2011, 14:58
  3. docked widgets and custom title bars
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 16th March 2011, 09:12
  4. Grouping of Tool buttons in Tool bar
    By febil in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2009, 11:51
  5. Tool Buttons...
    By csvivek in forum Qt Tools
    Replies: 1
    Last Post: 9th May 2008, 07:44

Tags for this Thread

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.