Make a Qt/C++ program show its file types as known on Windows

天大地大妈咪最大 提交于 2019-12-11 17:55:13

问题


Using Qt 5.9 I codded a spreadsheet program and then created an installer for it by Qt Installer Framework (QtIFW2.0.1). Then I sent the program to many of my friends. They installed the app on their Windows machine and now using it, but they have all have a common problem:
when they save files of the app, those files are shown as "unknown" files on Desktop.

The problem is only with the shape and appearance of the stored files not their functionality, and they are opened by the app if double clicked.

The question is, what changes in the code is needed to have the program make its files' shape/appearance shown known?
For example, we offer the code a specific shape using an image file or like that, to be mapped on the stored files and that way they are shown known.


回答1:


This has actually nothing to do with Qt or C++ itself. You just need to register your file extension in Windows shell, so it can be understood by other Windows components/shells.

Here is general information about File Types and File Associations under windows.

You need to make some Windows Registry entries which look like this:

example.reg:

Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Classes\myfirm.myapp.v1\shell\open\command]
    @="c:\path\to\your\app.exe \"%1\""
    [HKEY_CURRENT_USER\Software\Classes\.myextension]
    @="myfirm.myapp.v1"

Here you can read how it works in general

change myfirm.myapp.v1, .myextension and path to your .exe to your prefered names. Now Windows will know what the files with extension .myextension should be opened by your app. And if you double click on this files your app will be run with path to file as an argument. You can get it in your main() function

To set icon for your extension add Registry entry in Software\\Classes\\.myextension\\DefaultIcon and set it default value to the full path to your app, so windows can get an icon for extension from your .exe app file.

You can also do it at runtime directly in your app:

QSettings s("HKEY_CURRENT_USER\\SOFTWARE\\CLASSES", QSettings::NativeFormat);
    s.setValue(".myextension/DefaultIcon/.",QDir::toNativeSeparators(qApp->applicationFilePath()));
    s.setValue(".myextension/.","myfirm.myapp.v1");
    s.setValue("myfirm.myapp.v1/shell/open/command/.", QDir::toNativeSeparators(qApp->applicationFilePath()) + " %1");

EDIT: One more, to do it with Qt Installer look at the answers here



来源:https://stackoverflow.com/questions/46218044/make-a-qt-c-program-show-its-file-types-as-known-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!