6

QApplication's constructor takes an (int argc, char**argv) to handle any Qt specific commandline arguments.

What if my app is in unicode? And I have a wchar_t** argv?

It seems a bit silly to create a char* copy of all the commandline args to pass to a library that is itself unicode.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263

3 Answers3

10

Yes, it would be. If it wasn't for this note:

Warning: On Unix, this list is built from the argc and argv parameters passed to the constructor in the main() function. The string-data in argv is interpreted using QString::fromLocal8Bit(); hence it is not possible to pass, for example, Japanese command line arguments on a system that runs in a Latin1 locale. Most modern Unix systems do not have this limitation, as they are Unicode-based.

On NT-based Windows, this limitation does not apply either. On Windows, the arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the arguments() are constructed from the return value of GetCommandLine(). As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started.

Admittedly, I don't get the word either.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Sounds like: Most Unix systems do not prevent you from using arguments from any language you want for this reason, and NT Windows systems don't prevent you either, but for a different reason. – aschepler Nov 01 '10 at 19:03
  • So does that mean I can just pass the wchar version with a cast, or just ignore the args and pass NULL? – Martin Beckett Nov 01 '10 at 19:04
  • Just use `int main(int argc, char**argv)` and pass that. That works on both Unix and Windows (and OSX). Forget that `wmain` oddity. – MSalters Nov 02 '10 at 12:53
  • Well, if you'd ignore the WinMain() oddity. Too many programs already ignore the nShowCmd argument, what's another one. – Hans Passant Nov 02 '10 at 13:11
  • I can't, Google now uses this post as the ranking hit. It was rewritten, it now recommends that a program that targets Linux explicitly uses setlocale(). – Hans Passant Sep 07 '16 at 21:38
  • FWIW, the new link: http://doc.qt.io/qt-5/qcoreapplication.html#arguments – max630 Nov 25 '18 at 09:06
0

Well, main will always get char** argv, so that's what QApplication expects. You can also convert them (using what locale/encoding?) to wide strings if you want to do other things with the command arguments.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • Huh. Didn't know about that -- because it's a Windows-only thing. But then the function isn't `main` any more. – aschepler Nov 01 '10 at 19:12
  • Solaris used to have a wmain - hadn't realized that Linux doesn't have unicode commandline. – Martin Beckett Nov 01 '10 at 19:40
  • 1
    Well, a UTF-8 locale (and therefore a UTF-8 command line) is becoming more and more common as a Linux default. Which is Unicode and keeps things using `char*`. – aschepler Nov 01 '10 at 19:42
0

Just link qtmain library.

qtmain is a helper library that enables the developer to write a cross-platform main() function on Windows. If you do not use qmake, qbs, or other build tools such as CMake, then you need to link against the qtmain library.

https://doc.qt.io/qt-5/qtmain.html

Capric Tin
  • 11
  • 1