Qt World Summit: Register Today!

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

  • 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 ;
                    int nfichiers=argc-1;
                    int l=0;

    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");

    but nothing works.
    has someone an idea ?

  • Lifetime Qt Champion


    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.

  • @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...

  • 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");
    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...

  • @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 .

Log in to reply