Have to put an icon on file extensions of a program on Windows explorer
-
@tomy I know what you want to do. On windows this is done via registry, so out of scope of Qt. You need to find out what exactly you need to change in Windows registry. See MSDN.
The icon is associated with a file type/extension. -
@tomy
Windows registers icons for file types only by extensions, it doesn't check byte sequences in files' headers like Linux.
Thus, use WinAPI to edit registry from code on the installation stage of your program. Embed this functionality directly to the application - it's a bad practise. Only an installator must take responsibility for application's integration with a system. -
I recently answered similar question: How to set icon for a custom type of file using Qt.
As @Tikani mentioned this should probably be done in the installation stage, i.e. in some code invoked by the installer if it doesn't provide that functionality out of the box. Assuming, of course, that you have an installer in the first place.
-
-
I recently answered similar question: How to set icon for a custom type of file using Qt.
Glad to hear that. I will go for reading it precisely.
As @Tikani mentioned this should probably be done in the installation stage
Yes, exactly.
, i.e. in some code invoked by the installer if it doesn't provide that functionality out of the box. Assuming, of course, that you have an installer in the first place.
I use the Qt Installer Framework 2.1 (QtIFW2.0.1) for making an installer.
-
@Chris-Kawa
I included "shlobj.h" and used QSettings as below in the two functions where QSettings was previously used (because I was not sure where to use your code in):void MainWindow::readSettings() { QSettings settings("Tomy Inc.", "Spreadsheet"); restoreGeometry(settings.value("geometry").toByteArray()); recentFiles = settings.value("recentFiles").toStringList(); updateRecentFileActions(); bool showGrid = settings.value("showGrid", true).toBool(); showGridAction->setChecked(showGrid); bool autoRecalc = settings.value("autoRecalc", true).toBool(); autoRecalcAction->setChecked(autoRecalc); QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.sp\\DefaultIcon", QSettings::NativeFormat); reg.setValue("Default", "C:\\Users\\Tomy\\Desktop\\package_directory" "\\packages\\com.vendor.product\\data\\Spreadsheet.ico"); SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); } //************************************************************* void MainWindow::writeSettings() { QSettings settings("Tomy Inc.", "Spreadsheet"); settings.setValue("geometry", saveGeometry()); settings.setValue("recentFiles", recentFiles); settings.setValue("showGrid", showGridAction->isChecked()); settings.setValue("autoRecalc", autoRecalcAction->isChecked()); QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.sp\\DefaultIcon", QSettings::NativeFormat); reg.setValue("Default", "C:\\Users\\Tomy\\Desktop\\package_directory" "\\packages\\com.vendor.product\\data\\Spreadsheet.ico"); SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); }
Then changed the installscript.qs file to its previous status:
function Component() { // default constructor } Component.prototype.createOperations = function() { // call the base create operations function component.createOperations(); if (installer.value("os") == "win") { var userProfile = installer.environmentVariable("USERPROFILE"); installer.setValue("UserProfile", userProfile); component.addOperation("CreateShortcut", "@TargetDir@/Spreadsheet.exe", "@UserProfile@/Desktop/Spreadsheet.lnk" ,"workingDirectory=@TargetDir@", "iconPath=@TargetDir@/Spreadsheet.ico"); component.addOperation("CreateShortcut", "@TargetDir@/QSpreadsheet.exe", "@TargetDir@/Spreadsheet.lnk" ,"workingDirectory=@TargetDir@", "iconPath=@TargetDir@/Spreadsheet.ico"); component.addOperation("CreateShortcut", "@TargetDir@/Spreadsheet.exe", "@StartMenuDir@/Spreadsheet.lnk", "workingDirectory=@TargetDir@", "iconPath=@TargetDir@/Spreadsheet.ico"); } }
Then created an installer and installed it on Windows. But unfortunately no icon was set!!
Should I add this code to the script file too?
-
I this time removed the code:
QSettings reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.sp\\DefaultIcon", QSettings::NativeFormat); reg.setValue("Default", "C:\\Users\\Tomy\\Desktop\\Spreadsheet.ico"); SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
from both void MainWindow::readSettings() and void MainWindow::writeSettings() functions and added it to the MainWindow's constructor body, then re-did the rest and installed that new on my Windows.
Again, no changes in result!!
There might be a problem either in the code or the script file. -
I also tried to use this script file instead of the prior one. Created an installer using Qt Installer Framework but when installing the program faced en error.
I don't know what the right way is to do the job. Is it using that code? Or the script file? Or both of them? And what is the correct method having instructions to make the task done?
-
@jsulm
I think MSDN talks about setting the icon on my Windows. So if I send the application for you, you too should set the icon on your Windows, and if we send the application for a hundred people, those a hundred plus we two should set the icon and so on.
But if we set the icon using some method by the installation stage of the program, the program "itself" sets the icon for each and every user. No need to that 102 (or any number) persons set the icon themselves (apart from having good knowledge to be able to use registery that some users might not have).
If you disagree please tell me your opinion. -
@tomy Wrong. Why do you think so?
You need to use win32 API to achieve what you want in your app, so your app can set the icon by itself (and not the user!) or the installer.
win32 API is not Qt that's why I suggest to look at MSDN to find out how ann app or installer can do this.
MSDN is for developers not users by the way. -
Wrong. Why do you think so?
You need to use win32 API to achieve what you want in your app, so your app can set the icon by itself (and not the user!) or the installer.
win32 API is not Qt that's why I suggest to look at MSDN to find out how ann app or installer can do this.
MSDN is for developers not users by the way.But I even don't know what win32 API is and haven't been using it or MSDN up to now. Should I post a question there like a forum or how?
By the way, thanks for your info.
-
@tomy Actually you probably do not have to use win32 API for this as you can access the Windows registry via QSettings for example. But you need to know what to change in Windows registry and that is something out of Qts scope (and I don't know either). You can search on the internet or ask on MSDN or any other Windows development related forum/mailing list.
-
Actually you probably do not have to use win32 API for this as you can access the Windows registry via QSettings for example. But you need to know what to change in Windows registry and that is something out of Qts scope
I fear if I post the question on MSDN they say "it's not completely related to Windows and ask a Qt forum"! :)
Windows development related forum/mailing list.
How to gain assistance from Qt mailing list?
-
@tomy said in Have to put an icon on file extensions of a program on Windows explorer:
I fear if I post the question on MSDN they say "it's not completely related to Windows and ask a Qt forum"! :)
Why? You even don't have to mention Qt at all. Just ask how to assign an icon to a file type (you can mention that you're using C++). It is completely related to Windows as Registry is a Windows only thing.
I would not ask on Qt mailing list as it is more for people developing Qt.
-
So I think I should use something like that: (My extension is .sp)
Step1:QSettings reg("HKEY_CLASSES_ROOT\.sp\DefaultIcon", QSettings::NativeFormat);
Step2:
reg.setValue("Default", "C:\Users\Tomy\Desktop\Spreadsheet.ico");
Step3:
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
It's simplified compared to the previous one.