Comments, Code and Qt. Some words about the wonderful world of software engineering

18Sep/115

Easy threading with QtConcurrent::run()

Posted by kypeli

The following scenario is probably familiar to you: you are doing some heavy computing or IO intensive things in your app, like accessing a database, the filesystem or reading data from the Internet, that blocks your UI thread which leads to that the UI freezes. If you are writing a Qt app then probably the first thing that comes to your mind is to subclass QThread and implement the run() method (although you should not do that). But before you do that, you should take a look at Qt Concurrent and what it can offer. There is a good chance that what you need is already provided by the Qt Concurrent framework.

How I added threads to my app in 20 minutes

In this post I want to show you how I added threaded database access to my small podcast application for MeeGo, Podcatcher for N9. The use case I had at hands was that I added automatic cleanup of podcast episodes to Podcatcher for N9, which required some heavy database and filesystem access. This is the (naive) code (in short) that I originally had when cleaning up podcast episodes: QList channels = m_channelsModel->channels(); foreach(PodcastChannel *channel, channels) { PodcastEpisodesModel *episodesModel = m_episodeModelFactory->episodesModel(channel->channelDbId()); episodesModel->cleanOldEpisodes(m_keepNumEpisodesSettings, m_autoDelUnplayedSettings); }

Technorati Tags: , , , ,