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. How to set icon for a custom type of file using Qt or any c++ library
QtWS25 Last Chance

How to set icon for a custom type of file using Qt or any c++ library

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 3.6k Views
  • 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.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    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

    m.sueM 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Its via the OS native api.

      1 Reply Last reply
      2
      • AmrCoderA AmrCoder

        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

        m.sueM Offline
        m.sueM Offline
        m.sue
        wrote on last edited by
        #3

        Hi @AmrCoder

        Where do you want to see this icon? In a file dialog of the operating system or a file dialog of Qt? The latter you can probably achieve with the class QFileIconProvider, see http://doc.qt.io/qt-5/qfileiconprovider.html

        -Michael.

        1 Reply Last reply
        1
        • AmrCoderA Offline
          AmrCoderA Offline
          AmrCoder
          wrote on last edited by
          #4

          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

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

            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);
            
            1 Reply Last reply
            6
            • AmrCoderA Offline
              AmrCoderA Offline
              AmrCoder
              wrote on last edited by
              #6

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

              reg.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 code

              QStringList list = reg.allKeys();
              

              but it returns an empty list
              Thank you

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

                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 key reg.remove("Default"); can't work. To remove the value means to remove the key, so in your case you need to remove the DefaultIcon 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 so reg.allKeys() won't list it.

                1 Reply Last reply
                6

                • Login

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