开发者

How to open a file which has name with unicode symbols

I created an .exe file and associated .myFile extension to that .exe f开发者_运维百科ile. 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.


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜