[Qt-interest] QAbstractItemModel data supplied by a different thread.

Brad Howes howes at ll.mit.edu
Tue Jul 27 16:57:15 CEST 2010


On Jul 27, 2010, at 7:45 AM, Justus Best wrote:

> I trying to implement a fancy looking kinetic scrollable ListView. The 
> fancy looking and the kinetic scrolling I could solve by my self, but 
> the images I'm displaying have to be scaled first which leads to 
> performance problems if I do the scaling in the data function. I hoped 
> that using some functions from QtConcurrent could solve my problem, but 
> I can't exactly find out how to bring this together.
> What would be the "right" solution to get the job done, should I display 
> placeholder images at first and call 
> QtConcurrent::run(someScalingFunction...) install a QFutureWatcher on 
> the result and emmit the dataChanged-Signal every time a new Pixmap is 
> ready ? Or is there another solution.
> I'm open for any suggestions or some examples where I can look up how to 
> do this right.


I would use a thread pool, populated with some number of threads proportional to the number of processors you have. For the scaling, create a work request class that will hold the parameters necessary to define the image to scale, the scaling to perform, and whatever values are necessary to communicate to the widget where the image lives in its view. Create a consumer/producer queue for the thread pool that will hold the work requests. For all images in the view widget, create a work request with appropriate parameters and add to the queue. The worker threads are all waiting to consume from the queue. When a worker thread gets a work request object, it performs the necessary scaling, and when done, emits a thread-safe signal to the view widget with the appropriate parameters telling the view widget what image was scaled as well as the scaled image itself.

The QtConcurrent library may have some of the above for you to use; I don't know since I have not used it yet.

As for an example, I believe the Qt Mandelbrot example uses threads for rendering sections of the scene.

Brad

-- 
Brad Howes
Group 42
MIT Lincoln Laboratory • 244 Wood St. • Lexington, MA 02173
Phone: 781.981.5292 • Fax: 781.981.3495 • Secretary: 781.981.7420









More information about the Qt-interest-old mailing list