Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encoding problems with the arguments of a Qt application under windows 7
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.8k Views
  • 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 Offline
    S Offline
    stephane78
    wrote on last edited by A Former User
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • S Offline
        S Offline
        stephane78
        wrote on last edited by stephane78
        #3

        @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
        0
        • S Offline
          S Offline
          stephane78
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            stephane78
            wrote on last edited by stephane78
            #5

            @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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved