Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Custom icons in directory browser?
Moschops last edited by
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.
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":http://qt-project.org/doc/qt-4.8/qfileiconprovider.html and "set it to the QFileDialog":http://qt-project.org/doc/qt-4.8/qfiledialog.html#setIconProvider.
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.