Results 1 to 13 of 13

Thread: QMessageBox encoding problem

  1. #1

    Post QMessageBox encoding problem

    Hello.
    I've got a problem with my QMessageBox, special characters like "É" or "ç" are replaced by a "?" with a black background. I've read numerous posts in vain. I've tried that:
    Qt Code:
    1. QTextCodec::setCodecForLocale(QTextCodec::codecForName("ISO 8859-1"));
    To copy to clipboard, switch view to plain text mode 
    in my main.cpp file. I've also tried to change encoding in the Advanced Save Options but it's not working.
    What bothers me is that special characters are displayed correctly in the rest of my application (menus etc).
    Any ideas about how I could solve this problem?
    Thanks for your replies,
    Cheers.

  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: QMessageBox encoding problem

    If your text file is saved with UTF-8 encoding and your compiler does not do something silly with "non-ASCII" characters then this:
    Qt Code:
    1. QString thingy = QString::fromUtf8("Où sont mes accents?"); // French
    To copy to clipboard, switch view to plain text mode 
    should work. If the text file is saved with the ISO8859-1 Western European encoding then:
    Qt Code:
    1. QString thingy = QString::fromLatin1("¿Dónde están mis acentos?"); // Spanish
    To copy to clipboard, switch view to plain text mode 
    In the worst case you might have to manually encode the characters:
    Qt Code:
    1. // Portguese: Onde estão meus acentos?
    2. QString thingo = QString::fromUtf8("Onde est\u00E3o meus acentos?"); // works with gcc but not IIRC Microsoft compilers
    3.  
    4. QString thingy = QString::fromUtf8("Onde est\xC3\xA3o meus acentos?"); // should work with either
    5.  
    6. QString foo = QString::fromUtf8("Nerede benim aksan vard\u0131r?"); // Turkish (dotless i)
    7. QString bar = QString::fromUtf8("Nerede benim aksan vard\xC4\xB1r?");
    To copy to clipboard, switch view to plain text mode 
    ISO8859-1 does not completely support languages like Turkish. I used http://people.w3.org/rishida/tools/conversion/ to encode.

    If they are fixed messages or control labels then you could enter them in English and use the translation mechanism to provide translated versions.

    Without knowing what platform, compiler, languages, or code is involved it is hard to be more specific.

    BTW: Apologies to native French, Spanish, Portuguese, and Turkish speakers for the Google Translate examples

  3. #3

    Default Re: QMessageBox encoding problem

    Thanks for your reply.
    However, my QMessageBox text isn't in a text file. I'm writing it when I create my QMessageBox.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem


  5. #5
    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: QMessageBox encoding problem

    Quote Originally Posted by Yaoming View Post
    However, my QMessageBox text isn't in a text file. I'm writing it when I create my QMessageBox.
    Yes. None of the options I presented have anything to do with a text file other than the one your source code is in.

  6. #6

    Default Re: QMessageBox encoding problem

    Sorry I misunderstood your reply.
    When I put QTextCodec, it only gives me access to setCodecForLocale. I can't see CodecforTr or codecforCstrings. I've included qtextcodec.h though.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem


  8. #8

    Default Re: QMessageBox encoding problem

    Yeah I tried that, I can't see those functions.
    I've got QT5.2. I've included:
    Qt Code:
    1. #include "qtextcodec.h"
    To copy to clipboard, switch view to plain text mode 
    like that but I only have "setCodecForLocale and it's not correcting my special characters problem.

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem

    Yes, in Qt 5.2 API for codec is other than in 4.8.
    By the way if You set codecForLocale to ISO 8859-1 the text in the code should be coded in ISO 8859-1. Maybe show us some real code.

  10. #10

    Default Re: QMessageBox encoding problem

    I've solved my problem.
    Thank you all for your answers, they really heped me

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem

    This show off how you did it - share your knowledge with others.

  12. #12
    Join Date
    Mar 2016
    Location
    Ribeirao Preto, SP, Brazil
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem

    Hi Yaoming,

    How did you manage to solve your problem?

    I think I have a similar issue using QMessageBox::warning, my question text contains "é" character, but when the application runs it presents "Ãi" instead.

    I have tried:
    - set locale and enconding to pt_Br and UTF-8
    - passing my text string with tr and directly

    Nothing seems to work.
    Please anyone could help?

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox encoding problem

    Under the assumption of using Qt5, is your source code file encoded in UTF-8?

    Cheers,
    _

Similar Threads

  1. Qt5 + qdbf encoding problem
    By folibis in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2013, 22:02
  2. Encoding problem
    By newb_developer in forum Qt Programming
    Replies: 4
    Last Post: 4th July 2012, 19:03
  3. encoding problem
    By lexqqq in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 23:41
  4. Encoding problem with QTextEdit
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 2nd September 2009, 11:49
  5. qt and mysql encoding problem
    By ferasodh in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2007, 09:48

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.