3

I am new on QT and I have a problem with encoding. This is my main function

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    const char *charArray = "çşüğ";

    QStrıng str1(charArray);
    QString str2(argv[1]);
}

argv[1] value is the same with charArray on the caller exe side. But If I control the str1 and str2 values str1 is looking çşüğ but str2 ıs .!?><. I think it is because of encoding type. How can I fix this problem? Caller exe is c# application.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
doğan
  • 339
  • 1
  • 4
  • 15
  • Looks like a duplicate of ["QApplication in unicode"](http://stackoverflow.com/questions/4072016/qapplication-in-unicode) – Nikita Sep 07 '16 at 21:27
  • Using non-ASCII characters in a string literal is not a standard feature, so you can't know for sure what bytes actually end up in `charArray`. – roeland Sep 07 '16 at 21:59
  • It doesn't really have anything to do with C# directly, although that bit of information lets us know that this is specific to the Windows platform (though that should have been explicitly specified either in the question or the tags). – Dan Korn Sep 08 '16 at 17:50

2 Answers2

4

Use QCoreApplication::arguments For example

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if (argc > 1)
    {
        QString str1 = "çşüğ";
        QString str2 = a.arguments().at(1);
    }
    return a.exec();
}

Note that in Windows environment Unicode refers to UTF-16 standard, you need wide char strings, example const wchar_t *charArray = L"çşüğ"; While in Unix based environment Unicode refers to UTF-8.

Qt is supposed to be cross platform compatible so it tries to hide these differences through QString and other functions.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
0

Change the first line to:

int wmain(int argc, wchar_t *argv[])

Then call GetCommandLineW.

Dan Korn
  • 1,274
  • 9
  • 14
  • 1
    `GetCommandLineW` will return the program arguments as Unicode regardless of whether you use `main` or `wmain` or `WinMain`. – roeland Sep 07 '16 at 21:54
  • True, I guess I should have said "**OR** call GetCommandLineW." – Dan Korn Sep 07 '16 at 21:59
  • 1
    @DanKorn It's not possible to use `wmain` here. [`QApplication`](http://doc.qt.io/qt-5/qapplication.html) class doesn't have constructor with `wchar_t**` parameter. – Nikita Sep 08 '16 at 05:03
  • 1
    @Nikita: Those two statements are not causally linked. It's true that QApplication doesn't have a constructor that takes a wchar_t**. But that doesn't mean that you can't use wmain. – Dan Korn Sep 08 '16 at 16:40
  • @DanKorn `QApplication` is used in context of this question, it's central part of QT application. So it's critical to construct proper `QApplication` instance, but it's not possible with `wchar_t**` parameter. – Nikita Sep 08 '16 at 17:02
  • Yes, QApplication is a central part of a QT application, but its initialization does not seem to be pertinent to the original question here, which is "how can I get QT main function argv parameter as unicode." If you use wmain, or call GetCommandLineW, then you will indeed get the main function argv parameters as Unicode, which, I submit, answers the question posed. – Dan Korn Sep 08 '16 at 17:48