Unicode Problem..



  • hi everybody. i want use unicode in qt Creator my code is

    @
    #include <QtGui>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QPushButton btn( QString::fromUtf8("بستن") ); // unicode
    btn.show();
    
    return app.exec&#40;&#41;;
    

    }
    @

    its right in qt creator but when i run project this unicode will be shown ambigues font !


  • Moderators

    Does the default font that is being used to display the text on your OS when you run the app support those characters?



  • yes . my windows is 7 64bit and set to region and language -> language for non-unicode program to this language.



  • Are you sure the following code
    @
    char str[] = "بستن";
    @
    will get utf8 encoded bytes of "بستن"?


    First, your source code should be saved as utf8 with BOM.

    Then add following lines to your souce code.(If you are using MSVC2010)
    @
    #ifdef Q_CC_MSVC
    #pragma execution_character_set("UTF-8")
    #endif
    @

    If you are using MinGW, nothing special need to do.(with or without BOM)

    If you are using MSVC2005 or MSVC2008, I don't know how to do.

    If you are using MSVC2003, save source code as utf8 without BOM should works.

    ....



  • i use visual 2008 pro sp1. yes im sure encoded bytes of "بستن". i try to use mingw but i cant download it and i dont know how i must to use mingw or set qt option to use it ! if you know please help me.



  • Hi,

    IMO, this is pure C++ problem.

    If you are not sure that the execution_character_set of your compile environment is utf8, you can give a try to

    @
    char str[] = "\xd8\xa8\xd8\xb3\xd8\xaa\xd9\x86"; //I don't know whether it is the right utf8 bytes ;-)
    QString::fromUtf8(str);
    @

    instead of
    @
    char str[] = "بستن";
    QString::fromUtf8(str);
    @

    The formal will works in all the compilers, while the latter will depend on which compiler you choose.

    I do not know how to make the latter works under MSVC2005 or MSVC2008. In fact, I can not achieve this using Chinese-Version-of-MSVC2008. that's why MSVC2010 support

    @
    #pragma execution_character_set
    @

    In addition, if your compile support C++11, you can write

    @
    char str[] = u8"بستن";
    QString::fromUtf8(str);
    @



  • Maybe:

    @QPushButton btn( QString:::fromWCharArray(L"بستن") );@
    @QPushButton btn( QString::fromUtf16((const ushort*) L"بستن") );@



  • Yes, wchar_t is good option before compilers providing full support for char16_t and char32_t .



  • If fact, QStringLiteral can be used under Qt5.

    @
    QString str = QStringLiteral("بستن");
    @

    depend on which compiler used, it will use

    @
    char16_t* & u"بستن"
    @
    or
    @
    wchar_t* & L"بستن"
    @
    or
    @
    ushort*
    @
    or
    @
    char*
    @



  • tnx every body for your help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.