Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Encoding problems with the arguments of a Qt application under windows 7

    General and Desktop
    2
    5
    1541
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stephane78 last edited by A Former User

      Hi,
      I use the class SingleApplication for my purpose in a Qt software under windows:

      SingleApplication(int &argc, char *argv[], const QString uniqueKey);  
      
      ......
      SingleApplication::SingleApplication(int &argc, char *argv[], const QString uniqueKey) : QApplication(argc, argv)  
      

      In my main.cpp :

      SingleApplication app(argc, argv,"Mysoftware");
       if (app.alreadyExists())
              {
                  QString filename ;
                  
                  if(argc>1)
                  {
                      int nfichiers=argc-1;
                      int l=0;
                      while(l<nfichiers)
                      {
                          //filename=argv[l+1];
      
                          filename=QString::fromUtf8(argv[l+1]);
      
      

      I have added an entry in the contextual menu of windows (in the registry) that calls mysoftware with a selected file as argument : mysoftware %1
      if I select a file with special characters (é,è,à, because I have a french keyboard and a french windows 7) it doesn't run well and I have an encoding problem.
      If replace QString::fromUtf8 by QString::fromLatin1 or QString::fromLocal8Bit it crashes the software with files with special characters (haven't already debugged that).
      What is the encoding of the explorer on a french windows 7 ?
      I have tried several things like :

      QTextCodec *codec = QTextCodec::codecForName("System");
          QTextCodec::setCodecForLocale(codec);
      filename=QString::fromLocal8Bit(argv[l+1]);
      

      but nothing works.
      has someone an idea ?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Are you sure you need to do that conversion ? What do you get if you print the value of your arguments ?

        By the way, there's the QtSingleApplication class from the QtSolution module that is a cross-plaform single instance application handler.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • S
          stephane78 last edited by stephane78

          @SGaist, Ok thank you I will perhaps later try with QtSingleApplication of QtSolution instead of SIngleApplication.
          If I print the first arguments I must transform the char *, argv[1] into a QString to display it in a qmessagebox .if I do
          QString cs=argv[1]; (which is the same as cs=QString::fromUtf8(argv[1])) and then QMessageBox ..., there is an encoding problem...
          and even if I use QtSingleApplication, I must do that, i.e. transmit the arguments (char *) to a QString...
          so I think windows is unicode and transmit wide char to the Qt program but the Qt program take char * as parameters...

          1 Reply Last reply Reply Quote 0
          • S
            stephane78 last edited by

            Hi Sgaist, OK you have had a good idea with the message box.so there was probably two problems : if I want to display the right string in the messagebox I must do :

            QTextCodec *codec = QTextCodec::codecForName("System");
                QTextCodec::setCodecForLocale(codec);
            QString cs=codec->toUnicode(argv[1]);
            			QMessageBox::information(NULL,"arg 1",cs);
            

            so I will use the three first lines now...
            and there must be another problem with my class SingleApplication and now I will try to use the last version of QtSingleApplication instead of this class.
            I will come back here when it works...

            1 Reply Last reply Reply Quote 0
            • S
              stephane78 last edited by stephane78

              @SGaist , ok my problem is solved now thanks to these three lines and thanks to the class QtSingleApplication of Qtsolutions.
              If I use a french windows it takes the special french characters (é,è,à) in the name of selected files ,through the contextual menu of windows .

              1 Reply Last reply Reply Quote 0
              • First post
                Last post