pass unicode arguments to QApplication











up vote
1
down vote

favorite












I have windows console application which accepts unicode characters as argument



 wmain( int argc , wchar_t ** argv)


Now I want to pass the argv argument to my QApplication . However the QApplication accepts on char ** .



How do I manage to achieve this ?










share|improve this question






















  • Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
    – Scheff
    Nov 22 at 13:53










  • IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
    – MSalters
    Nov 22 at 14:29






  • 1




    according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
    – max630
    Nov 25 at 8:38















up vote
1
down vote

favorite












I have windows console application which accepts unicode characters as argument



 wmain( int argc , wchar_t ** argv)


Now I want to pass the argv argument to my QApplication . However the QApplication accepts on char ** .



How do I manage to achieve this ?










share|improve this question






















  • Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
    – Scheff
    Nov 22 at 13:53










  • IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
    – MSalters
    Nov 22 at 14:29






  • 1




    according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
    – max630
    Nov 25 at 8:38













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have windows console application which accepts unicode characters as argument



 wmain( int argc , wchar_t ** argv)


Now I want to pass the argv argument to my QApplication . However the QApplication accepts on char ** .



How do I manage to achieve this ?










share|improve this question













I have windows console application which accepts unicode characters as argument



 wmain( int argc , wchar_t ** argv)


Now I want to pass the argv argument to my QApplication . However the QApplication accepts on char ** .



How do I manage to achieve this ?







c++ qt unicode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 13:49









sameer karjatkar

88531232




88531232












  • Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
    – Scheff
    Nov 22 at 13:53










  • IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
    – MSalters
    Nov 22 at 14:29






  • 1




    according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
    – max630
    Nov 25 at 8:38


















  • Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
    – Scheff
    Nov 22 at 13:53










  • IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
    – MSalters
    Nov 22 at 14:29






  • 1




    according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
    – max630
    Nov 25 at 8:38
















Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
– Scheff
Nov 22 at 13:53




Either, you use main() instead or you "translate" wchar_ts to chars. I cannot imagine that QApplication expects command line arguments where non-ASCII values are relevant but actually I'm not sure. File paths would be the exception.
– Scheff
Nov 22 at 13:53












IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
– MSalters
Nov 22 at 14:29




IIRC QApplication::arguments on Windows is already Unicode-aware. Write portable code, using int main(int argc, char**argv) and Qt will magically make it work.
– MSalters
Nov 22 at 14:29




1




1




according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
– max630
Nov 25 at 8:38




according to this answer the constructor argumenrt are not used to construct QApplication::arguments(). So you could as well not bother.
– max630
Nov 25 at 8:38












3 Answers
3






active

oldest

votes

















up vote
1
down vote













Until QTBUG-3200 is fixed, the only way to not be bounded by the "Current locale for non-Unicode" charset seem to convert the arguments to UTF-8, then



QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
QApplication(argc, argv_converted);


I cannot verify currently that it would work, though.






share|improve this answer




























    up vote
    1
    down vote













    You don’t need to pass these arguments to QApplication constructor. You can pass a dummy array, since it’s not used on Windows. The C-style program argument passing via main arguments is not the Windows-native way of accessing arguments, and QApplication doesn’t use it. The wchar-based main is a kludge that attempts to distract from the fact that argv is not a native interface.



    This works:



    int argcc = 1;
    char arg0[1] = "";
    char *argvc[2] = {&arg0, nullptr};
    QApplication app(argcc, argvc);





    share|improve this answer




























      up vote
      -1
      down vote













      Qt do not use wchar_t* for Unicode strings as usual. Only QString or char*. If you want to contvert QString -> char* (or char* -> QString) use QTextCodec.
      For example, older versions of Windows has encoding cp866 for console and if you want send unicode QString into command line:



      QString string = "Unicode text";
      QTextCodec *codec = QTextCodec::codecForName("cp866");
      QByteArray encodedString = codec->fromUnicode(string);
      //encodedString.data() - char* representation of unicode QString


      For reading input parameters in main() convert like this:



      QTextCodec *codec = QTextCodec::codecForName("cp866");
      QString strArg = codec->toUnicode(argv);





      share|improve this answer























      • I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
        – MSalters
        Nov 22 at 14:26






      • 1




        Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
        – MSalters
        Nov 22 at 15:06











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432448%2fpass-unicode-arguments-to-qapplication%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Until QTBUG-3200 is fixed, the only way to not be bounded by the "Current locale for non-Unicode" charset seem to convert the arguments to UTF-8, then



      QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
      QApplication(argc, argv_converted);


      I cannot verify currently that it would work, though.






      share|improve this answer

























        up vote
        1
        down vote













        Until QTBUG-3200 is fixed, the only way to not be bounded by the "Current locale for non-Unicode" charset seem to convert the arguments to UTF-8, then



        QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
        QApplication(argc, argv_converted);


        I cannot verify currently that it would work, though.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Until QTBUG-3200 is fixed, the only way to not be bounded by the "Current locale for non-Unicode" charset seem to convert the arguments to UTF-8, then



          QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
          QApplication(argc, argv_converted);


          I cannot verify currently that it would work, though.






          share|improve this answer












          Until QTBUG-3200 is fixed, the only way to not be bounded by the "Current locale for non-Unicode" charset seem to convert the arguments to UTF-8, then



          QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
          QApplication(argc, argv_converted);


          I cannot verify currently that it would work, though.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 at 8:31









          max630

          5,17111340




          5,17111340
























              up vote
              1
              down vote













              You don’t need to pass these arguments to QApplication constructor. You can pass a dummy array, since it’s not used on Windows. The C-style program argument passing via main arguments is not the Windows-native way of accessing arguments, and QApplication doesn’t use it. The wchar-based main is a kludge that attempts to distract from the fact that argv is not a native interface.



              This works:



              int argcc = 1;
              char arg0[1] = "";
              char *argvc[2] = {&arg0, nullptr};
              QApplication app(argcc, argvc);





              share|improve this answer

























                up vote
                1
                down vote













                You don’t need to pass these arguments to QApplication constructor. You can pass a dummy array, since it’s not used on Windows. The C-style program argument passing via main arguments is not the Windows-native way of accessing arguments, and QApplication doesn’t use it. The wchar-based main is a kludge that attempts to distract from the fact that argv is not a native interface.



                This works:



                int argcc = 1;
                char arg0[1] = "";
                char *argvc[2] = {&arg0, nullptr};
                QApplication app(argcc, argvc);





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You don’t need to pass these arguments to QApplication constructor. You can pass a dummy array, since it’s not used on Windows. The C-style program argument passing via main arguments is not the Windows-native way of accessing arguments, and QApplication doesn’t use it. The wchar-based main is a kludge that attempts to distract from the fact that argv is not a native interface.



                  This works:



                  int argcc = 1;
                  char arg0[1] = "";
                  char *argvc[2] = {&arg0, nullptr};
                  QApplication app(argcc, argvc);





                  share|improve this answer












                  You don’t need to pass these arguments to QApplication constructor. You can pass a dummy array, since it’s not used on Windows. The C-style program argument passing via main arguments is not the Windows-native way of accessing arguments, and QApplication doesn’t use it. The wchar-based main is a kludge that attempts to distract from the fact that argv is not a native interface.



                  This works:



                  int argcc = 1;
                  char arg0[1] = "";
                  char *argvc[2] = {&arg0, nullptr};
                  QApplication app(argcc, argvc);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 at 8:47









                  Kuba Ober

                  69k982182




                  69k982182






















                      up vote
                      -1
                      down vote













                      Qt do not use wchar_t* for Unicode strings as usual. Only QString or char*. If you want to contvert QString -> char* (or char* -> QString) use QTextCodec.
                      For example, older versions of Windows has encoding cp866 for console and if you want send unicode QString into command line:



                      QString string = "Unicode text";
                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QByteArray encodedString = codec->fromUnicode(string);
                      //encodedString.data() - char* representation of unicode QString


                      For reading input parameters in main() convert like this:



                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QString strArg = codec->toUnicode(argv);





                      share|improve this answer























                      • I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                        – MSalters
                        Nov 22 at 14:26






                      • 1




                        Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                        – MSalters
                        Nov 22 at 15:06















                      up vote
                      -1
                      down vote













                      Qt do not use wchar_t* for Unicode strings as usual. Only QString or char*. If you want to contvert QString -> char* (or char* -> QString) use QTextCodec.
                      For example, older versions of Windows has encoding cp866 for console and if you want send unicode QString into command line:



                      QString string = "Unicode text";
                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QByteArray encodedString = codec->fromUnicode(string);
                      //encodedString.data() - char* representation of unicode QString


                      For reading input parameters in main() convert like this:



                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QString strArg = codec->toUnicode(argv);





                      share|improve this answer























                      • I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                        – MSalters
                        Nov 22 at 14:26






                      • 1




                        Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                        – MSalters
                        Nov 22 at 15:06













                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      Qt do not use wchar_t* for Unicode strings as usual. Only QString or char*. If you want to contvert QString -> char* (or char* -> QString) use QTextCodec.
                      For example, older versions of Windows has encoding cp866 for console and if you want send unicode QString into command line:



                      QString string = "Unicode text";
                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QByteArray encodedString = codec->fromUnicode(string);
                      //encodedString.data() - char* representation of unicode QString


                      For reading input parameters in main() convert like this:



                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QString strArg = codec->toUnicode(argv);





                      share|improve this answer














                      Qt do not use wchar_t* for Unicode strings as usual. Only QString or char*. If you want to contvert QString -> char* (or char* -> QString) use QTextCodec.
                      For example, older versions of Windows has encoding cp866 for console and if you want send unicode QString into command line:



                      QString string = "Unicode text";
                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QByteArray encodedString = codec->fromUnicode(string);
                      //encodedString.data() - char* representation of unicode QString


                      For reading input parameters in main() convert like this:



                      QTextCodec *codec = QTextCodec::codecForName("cp866");
                      QString strArg = codec->toUnicode(argv);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 at 14:17

























                      answered Nov 22 at 14:12









                      Serhiy Kulish

                      923




                      923












                      • I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                        – MSalters
                        Nov 22 at 14:26






                      • 1




                        Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                        – MSalters
                        Nov 22 at 15:06


















                      • I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                        – MSalters
                        Nov 22 at 14:26






                      • 1




                        Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                        – MSalters
                        Nov 22 at 15:06
















                      I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                      – MSalters
                      Nov 22 at 14:26




                      I think you should mention QChar*, which is the direct Qt alternative to wchar_t*.
                      – MSalters
                      Nov 22 at 14:26




                      1




                      1




                      Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                      – MSalters
                      Nov 22 at 15:06




                      Also note that code page 866 is Cyrillic; it's the default on a very small number of PC's worldwide.
                      – MSalters
                      Nov 22 at 15:06


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432448%2fpass-unicode-arguments-to-qapplication%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)