Important: Please read the Qt Code of Conduct -

Custom icons in directory browser?

  • I'm using a standard QFileDialog to let the user specify a directory. For this particular use case, the user must be able to traverse the directory tree as normal, but ultimately only certain directories are valid choices. These valid directories can be identified functionally; it would be ideal if I could change the icon of these directories so the user can easily see which directories are a valid choice.

    Is there a way to dictate at runtime the icon used for each directory, based on the results of a function? I had a root around in QFileDialog but didn't see anything.

  • Moderators

    yes it is possible, but only if you use QFileDialog without the static members, meaning you don't use the native file dialogs.

    For this you need to implement you custom "QFileIconProvider": and "set it to the QFileDialog":

    You need to reimplement QFileIconProvider::icon() (i'm not sure which one of the both are called, just test it). Probably the best would be to take icon from the base class (which QFileIconProvider gives you), get the QPixmaps out of the QIcon and paint your stuff on them and add them to a new QIcon which you return.

Log in to reply