Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Have to put an icon on file extensions of a program on Windows explorer
Forum Updated to NodeBB v4.3 + New Features

Have to put an icon on file extensions of a program on Windows explorer

Scheduled Pinned Locked Moved Unsolved General and Desktop
34 Posts 6 Posters 9.7k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT tomy

    @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.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #20

    @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.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    tomyT 1 Reply Last reply
    1
    • jsulmJ jsulm

      @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.

      tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by
      #21

      @jsulm

      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.

      jsulmJ 1 Reply Last reply
      0
      • tomyT tomy

        @jsulm

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #22

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        tomyT 1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #23

          @jsulm

          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?

          jsulmJ 1 Reply Last reply
          0
          • tomyT tomy

            @jsulm

            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?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #24

            @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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J.HilkJ 1 Reply Last reply
            1
            • jsulmJ jsulm

              @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.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #25

              @jsulm actually here is a detailed description on how to modify the Regestry to Assign a Custom Icon to a File Type


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by
                #26

                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.

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #27

                  @tomy said in Have to put an icon on file extensions of a program on Windows explorer:

                  C:\Users\Tomy\Desktop\Spreadsheet.ico

                  That path would only be valid on your machine , would it not?

                  Else the next user should also be Tomy :)

                  tomyT 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @tomy said in Have to put an icon on file extensions of a program on Windows explorer:

                    C:\Users\Tomy\Desktop\Spreadsheet.ico

                    That path would only be valid on your machine , would it not?

                    Else the next user should also be Tomy :)

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by
                    #28

                    @mrjj
                    I thought when I run the application, it copies that icon from that path and embeds it into the Spreadsheet.exe file which then will be used for creating the installer, making the other users not need to have such an icon on their Windows machines. :(

                    If it's not right, so what path should I put that icon and gives the statement its path?

                    1 Reply Last reply
                    0
                    • Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by Chris Kawa
                      #29

                      Wow, a lot of confusion here and the problem is actually pretty simple. There are 3 questions: the what, the where and the how.

                      The what - what you need to do on Windows to set an icon for a file type.
                      You need to set a registry key for that: HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5\\DefaultIcon and then force Windows to update icon cache.
                      Do not write to HKEY_CLASSES_ROOT directly.

                      The where - where to do it.
                      You can do it in couple of places:
                      In your app when it starts. Generally that's not a good idea as it's annoying that the app changes files settings every time it runs. A user might want to change that manually later and you should respect that.
                      The better place for that is the app's installer and depending on the installer you use it might give you that functionality or you'd need to do it yourself.

                      The how - how to do it
                      If you decide that your app should register the icon use the code that's been posted several times now:

                      QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.sp\\DefaultIcon", QSettings::NativeFormat);
                      reg.setValue("Default", "C:\\Path\\To\\some_icon.ico");
                      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
                      

                      As @mrjj noted - don't hardcode that path. Use some runtime detection of where it is located. There's no magic like automatic copying or embedding involved. The icon needs to be deployed to the user's machine along the executable.
                      You can also embed the icon in the executable manually and use that as the registry value. The syntax is a little different then.
                      If you want that say so and I'll tell you how.

                      If you decide that the installer should do that and you choose Qt installer framework the feature is provided by it. No need for c++ or winapi. Follow the example. In the Component.prototype.createOperations function add built-in operation "RegisterFileType"

                      component.addOperation("RegisterFileType",
                                             "sp",
                                             "@TargetDir@\\Spreadsheet.exe \" %1\"",
                                             "Tomy file",
                                             "some MIME type",
                                             "@TargetDir@/some_icon.ico",
                                             "ProgId=Whatever.");
                      

                      Adjust the strings of course. Again - no magic involved. The icon needs to be deployed along with the executable.

                      tomyT 1 Reply Last reply
                      5
                      • Chris KawaC Chris Kawa

                        Wow, a lot of confusion here and the problem is actually pretty simple. There are 3 questions: the what, the where and the how.

                        The what - what you need to do on Windows to set an icon for a file type.
                        You need to set a registry key for that: HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5\\DefaultIcon and then force Windows to update icon cache.
                        Do not write to HKEY_CLASSES_ROOT directly.

                        The where - where to do it.
                        You can do it in couple of places:
                        In your app when it starts. Generally that's not a good idea as it's annoying that the app changes files settings every time it runs. A user might want to change that manually later and you should respect that.
                        The better place for that is the app's installer and depending on the installer you use it might give you that functionality or you'd need to do it yourself.

                        The how - how to do it
                        If you decide that your app should register the icon use the code that's been posted several times now:

                        QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.sp\\DefaultIcon", QSettings::NativeFormat);
                        reg.setValue("Default", "C:\\Path\\To\\some_icon.ico");
                        SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
                        

                        As @mrjj noted - don't hardcode that path. Use some runtime detection of where it is located. There's no magic like automatic copying or embedding involved. The icon needs to be deployed to the user's machine along the executable.
                        You can also embed the icon in the executable manually and use that as the registry value. The syntax is a little different then.
                        If you want that say so and I'll tell you how.

                        If you decide that the installer should do that and you choose Qt installer framework the feature is provided by it. No need for c++ or winapi. Follow the example. In the Component.prototype.createOperations function add built-in operation "RegisterFileType"

                        component.addOperation("RegisterFileType",
                                               "sp",
                                               "@TargetDir@\\Spreadsheet.exe \" %1\"",
                                               "Tomy file",
                                               "some MIME type",
                                               "@TargetDir@/some_icon.ico",
                                               "ProgId=Whatever.");
                        

                        Adjust the strings of course. Again - no magic involved. The icon needs to be deployed along with the executable.

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by tomy
                        #30

                        @Chris-Kawa

                        If you decide that the installer should do that and you choose Qt installer framework the feature is provided by it. No need for c++ or winapi. Follow the example. In the Component.prototype.createOperations function add built-in operation "RegisterFileType"

                        component.addOperation("RegisterFileType",
                                               "sp",
                                               "@TargetDir@\\Spreadsheet.exe \" %1\"",
                                               "Tomy file",
                                               "some MIME type",
                                               "@TargetDir@/some_icon.ico",
                                               "ProgId=Whatever.");
                        

                        Adjust the strings of course. Again - no magic involved. The icon needs to be deployed along with the executable.

                        Thanks.
                        I this it's better among others.

                        It's code of the script file I've used so far:

                        function Component(){ }
                        
                        Component.prototype.createOperations = 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");
                              }
                         }
                        

                        I have two issues:
                        1- should I use the whole code of that link and embed it into this, or is it enough to add the operation below into it?

                        component.addOperation("RegisterFileType",
                                               "sp",
                                               "@TargetDir@\\Spreadsheet.exe \" %1\"",
                                               "Spreadsheet",
                                               "some MIME type",
                                               "@TargetDir@/Spreadsheet.ico",
                                               "ProgId=Whatever.")
                        

                        2- What's that MIME file?
                        Is that operation OK to use now?

                        1 Reply Last reply
                        0
                        • Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by Chris Kawa
                          #31

                          should I use the whole code (...)

                          Well no, the example is just that - an example. It creates a ui (a checkbox) for the user to choose if he wants to set the icon and then randomly generates some extension.
                          You don't need all that. Of interest to you is just the function that actually sets the thing. that's the component.addOperation(...) part.

                          MIME type is an identifier that globally describes your file type. See here. If it's your custom type you need to invent some new MIME type e.g. "application/tomy".

                          As for ProgId that's a bit more involved. I previously said that to set an icon you need to change that HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5\\DefaultIcon key.
                          Well that's a shortcut actually, as the "full" setup for this is to create two keys:
                          HKEY_CURRENT_USER\\SOFTWARE\\Classes\\SomeAppName.SomeType\DefaultIcon
                          and then refer to that name in the
                          HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.sp key by the name SomeAppName.SomeType.
                          It does the same thing but is better if you need more settings than just the file icon.
                          Qt Installer Framework uses that "full" setup, so the ProgId is that "SomeAppName.SomeType" identifier.

                          tomyT 1 Reply Last reply
                          1
                          • Chris KawaC Chris Kawa

                            should I use the whole code (...)

                            Well no, the example is just that - an example. It creates a ui (a checkbox) for the user to choose if he wants to set the icon and then randomly generates some extension.
                            You don't need all that. Of interest to you is just the function that actually sets the thing. that's the component.addOperation(...) part.

                            MIME type is an identifier that globally describes your file type. See here. If it's your custom type you need to invent some new MIME type e.g. "application/tomy".

                            As for ProgId that's a bit more involved. I previously said that to set an icon you need to change that HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.mp5\\DefaultIcon key.
                            Well that's a shortcut actually, as the "full" setup for this is to create two keys:
                            HKEY_CURRENT_USER\\SOFTWARE\\Classes\\SomeAppName.SomeType\DefaultIcon
                            and then refer to that name in the
                            HKEY_CURRENT_USER\\SOFTWARE\\Classes\\.sp key by the name SomeAppName.SomeType.
                            It does the same thing but is better if you need more settings than just the file icon.
                            Qt Installer Framework uses that "full" setup, so the ProgId is that "SomeAppName.SomeType" identifier.

                            tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by tomy
                            #32

                            @Chris-Kawa
                            Thanks.
                            So what I need I think is:
                            First, remove the QSettings code inside the program's code. Because I want to use the script file for the task not the program's code.
                            Second, use the script file with this code to create the installer:

                            function Component(){ }
                            
                            Component.prototype.createOperations = 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");
                                    
                                    component.addOperation("RegisterFileType",
                                                   "sp",
                                                   "@TargetDir@\\Spreadsheet.exe \" %1\"",
                                                   "Spreadsheet",
                                                   "Spreadsheet/sp",
                                                   "@TargetDir@/Spreadsheet.ico",
                                                   "ProgId=Spreadsheet.sp")
                                  }
                             }
                            
                            
                            

                            Do you agree?
                            I tested it and result is the same still!

                            1 Reply Last reply
                            0
                            • tomyT Offline
                              tomyT Offline
                              tomy
                              wrote on last edited by
                              #33

                              I don't know what part of the code is still incorrect.

                              1 Reply Last reply
                              0
                              • tomyT Offline
                                tomyT Offline
                                tomy
                                wrote on last edited by
                                #34

                                I might create a new thread focusing exactly at this point at the beginning, which seems to be the source of my issue.

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved