How to set icon for a custom type of file using Qt or any c++ library
-
I make an application in Qt which using a custom type of file to save the data of the application and I give it any extension for example
.mp5
or any other type
how can I set or assign an icon to this file when it exported from my application using Qt or any c++ library
Thanks in advance -
Hi
Its via the OS native api. -
-
I am using windows
and I want to to see in all system QT dialog and without QT dialog like any icon that appears on the files of any application for example excel, word, mp3 files have an icon I want to make an icon for my application like these files
@m-sue
about the QFileIconProvider class, I use it to give me the icon of the file but not set the icon to a file -
On Windows it's a two-part operation. See the docs: How to Assign a Custom Icon to a File Type.
So first set an icon for your file extension like this:QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5\\DefaultIcon", QSettings::NativeFormat); reg.setValue("Default", "C:\\Path\\To\\some_icon.ico");
This is a per-user setting. If you want it for all users change "HKEY_CURRENT_USER" to "HKEY_LOCAL_MACHINE", but your app will need elevated access for that.
After that you need to let the shell (explorer.exe) know that you changed something so it updates. Unfortunately Qt can't help you with that part so you need to reach to the native api:
#include "Shlobj.h" SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
-
@Chris-Kawa Thank you very much, it works great as I want exactly but I have more question please and the last one
what if I want to change it or remove it I tried to change it to use the same code but change the icon but it didn't work
I try to remove it by using this codereg.remove("Default");
nothing happened when I create a file with the extension the icon show on it
I tried to get all the keys by using this codeQStringList list = reg.allKeys();
but it returns an empty list
Thank you -
The philosophy of Windows registry is a little different than that of QSettings in that QSettings operates on keys and values, where in the registry a key is also a value. To facilitate this
"Default"
in QSettings is a sort of virtual entity that means "the value side of a key".
Since you can't remove the value without removing the keyreg.remove("Default");
can't work. To remove the value means to remove the key, so in your case you need to remove theDefaultIcon
key, not its "Default" value:QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5", QSettings::NativeFormat); reg.remove("DefaultIcon");
As for listing keys it's the same story -
"DefaultIcon"
is a key that has"Default"
value."Default"
is not a child key soreg.allKeys()
won't list it.