Locating Files

It's time for another lazy Saturday afternoon blog entry. Lazy because that's how I like Saturdays. No emails that need answering, no tasks that need fixing, just doing what I feel like. Sometimes hacking, sometimes making music, sometimes chatting. And almost traditionally some good Italian food. But let's digress towards a neat feature in Qt 4.3 that Girish and I implemented on Thursday.

Have you ever used QMimeSourceFactory in Qt 3? Yes yes, that mysterious class that tries to solve so many problems with a name that is totally unrelated to any of them. For example it helps to locate images embedded in HTML markup in labels and tooltips. You could just write ...

QMimeSourceFactory::defaultFactory()->addFilePath(QDir::homePath() + "/images");

QToolTip::add(widget, "


Foo ...");

... and when the tooltip is shown it would locate example.png in $HOME/images. That is because Qt 3's richtext module used QMimeSourceFactory to locate resources like that.

Now in Qt 4 we moved all the functionality that QMimeSourceFactory provided into their respective locations. For example through QTextBrowser's searchPaths() property or QTextDocument's addResource() / loadResource() functions. Or through the introduction of the Qt Resource System. Unfortunately we kind of overlooked the use-case where one wants to reference images on the filesystem from tooltips or labels with relative paths only. Of course it is possible to still make that working, for example by implementing a pseudo file engine. But that's not as convenient as one simple method call like in Qt 3. After quite a bit of discussion (with Jasmin and Andreas) we think we've found a nice solution. Say 'hello' to QDir's search paths:

QDir::addSearchPath("icons", QDir::homePath() + "/images");

widget->setToolTip("...by pressing the button ...");
QFile f("icons:thumbnails/landscape.png");
QStringList allThumbs = QDir("icons:thumbnails/").entryList();

I think this is mostly self-explaining :). Since it is implemented at the file engine level it works with anything that uses them, including QFile and QDir of course.

And as usual a note of warning: APIs in snapshots may change ;-). It's all subject to change, as much as the wheather here in Oslo. But while it is finally cooling down here (giving hope for snow after Christmas) Qt development is only heating up. I like it that way :)

I also observed that pictures help to grab people's attention for blogs. But since I can't really post a screenshot of the header file I decided to post a picture of the self-made pizza I've just had instead (with Pesto, Rucola, Buffalo Mozzarella, Avocado, Olives and Mushrooms):

Yummy self-made Pizza with plenty of Italian ingredients

Blog Topics: