开发者

Streaming QImages in Qt 4.7

I am building a GUI app in Qt with similar functionality to Google Maps (i.e. you can pan/zoom and more of the map gets displayed). At the moment I am dynamically loading 256 pixel blocks of the images as they are required (from the hard drive).

As you can imagine this slows down my program whenever I am panning and zooming. I want to use a separate thread (MapLoader) to load the 开发者_高级运维images as they are needed.

My data class has a triple pointer to the images (i.e. QImage* [x][y]). Images outside the viewport are null while those inside the viewport are QImage pointers.

My problem is, I want my painter to be able to access the array of images and draw them (regardless of whether or not they are loaded). While at the same time loading the images into the array without blocking access to the array.

How can I go about solving this problem? Does the array of images have to be volatile?


There is a nice demo on QtLabs (with video) of a test application that would be relevant to your situation. It is an image browser that loads images in a grid. Although the author states it is not an optimal solution, I think it should contain some helpful information for using threads to load content. You can see it here. I think there may be some interesting links in the comments section as well.


You should be able to spawn the loading of the images off to worker threads, and have those threads signal you somehow when an image is ready, along with the location of the image. (Using QtConcurrent would be my first inclination for this.) If you use the signal/slot connections, by default Qt will make sure a slot is run in its proper thread. So have your images class in the UI thread, have the worker threads emit signals with the images when they are read, and have the slot in the image class (still in the UI thread context) store the images properly. Since the assignment into the map and the reading from the map are in the same thread context, you shouldn't need to with any volatile markings or locking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜