tags:

views:

93

answers:

3

I created an .exe file and associated .myFile extension to that .exe file. I want to double click on any .myFile file and get that file opened by the .exe. For that I have done the following:

int main(int argc, char *argv[])
{
    QString fileName(QObject::tr(argv[1]));
    if ( fileName != "" )
    {
        mainWin.loadFile(fileName);
    }
.................. 
}

But when I have named my file in unicode characters (e.g. "Здравствуй.myFile"), the instead of "Здравствуй" you can see "????????". How to solve this problem? I know this is solved problem because, for example, MS Word does that.

+1  A: 

You have to use wmain instead of main on Windows:

int wmain(int argc, wchar_t** argv) {
    QString fileName = QString::fromWCharArray(argv[1]);  // untested

If you have to follow the C standard (which is all but useless on Windows), you can use GetCommandLineW and CommandLineToArgvW instead.

Philipp
If you say how to convert wchar_t** argv into char** argvCHAR then I can pass in to QApplication constructor.
Narek
Use MSalters' solution. `QApplication` does the right thing.
Philipp
A: 

Assuming the unicode you pass in is actually stored as UTF-8, try using QString::fromUtf8 to read the argument, something like this:

int main(int argc, char *argv[])
{
    QString fileName(QObject::trUtf8(argv[1]));
    if ( fileName != "" )
    {
        mainWin.loadFile(fileName);
    }
    // stuff
}

QObject::trUtf8 is actually a wrapper that will utilize QString:fromUtf8 and still perform the translation (even though i dont understand why you want to translate file names)

Hendrik
It is not stored as UTF-8 on Windows.
Philipp
+8  A: 

The previous answers that focus on int main(int argc, char** argv) are needlessly complex. Qt has a better alternative.

From the Qt documentation: On Windows, the QApplication::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().

So, the correct answer is to use qApp.arguments().at(1), which will give you the Unicode filename in a QString.

MSalters
And what argument I should pass to QApplication constructor in order to be able to call the function mentioned by you then.
Narek
What is qApp? Where i construct or get it?
Narek
See http://doc.qt.nokia.com/4.6/qapplication.html#qApp : All Qt apps should have that macro. You should pass the normal argc/argv from main. They're ignored on Windows, but it keeps your program portable to Mac and Linux
MSalters
OK, ok!! I got in, it is just the old budddy QApplication app(argc, argv); I didn't know that windows ignores that, thus it was strange. What about mac and linux, how they handle this situation do they ignore too?
Narek
On modern Unix-like systems, `char*` strings are usually UTF-8, which can be easily converted to Qt's UTF-16 strings.
Philipp
Again, see the first link in my answer. Continue to pass the arguments from `int main(int argc, char** argv)` to the QApplication constructor. On Mac and Linux, those will usually be UTF-8, and Qt will call `QString::fromLocal8Bit()` to do the UTF-8 to Qstring conversion.
MSalters