Following on the heels of last week’s post, here are a few more thoughts gleaned from the experience of attending last year’s Qt Developer Days. These are all old news, especially if you’ve been doing Qt for a while, but putting them here may be useful to you, and clears out a page of thoughts in my engineering notes!
QImage
vs. QPixmap
Remember that QImage
is the general-purpose class for image manipulation whose data lives in main memory, while QPixmap
objects are usually backed by the GPU. This has some ramifications for your application:
QImage
is optimized for I/O and direct pixel access.QPixmap
is optimized for direct screen display.- On constrained platforms like mobile devices, there may be relatively little GPU RAM available, so you should consider using
QPixmap
objects sparingly.
Regardless of which you choose, both classes use Qt’s copy-on-write (COW) semantics, so if you have multiple instances of the same image, you pay a small price penalty—basically the cost of the object overhead, not the memory footprint of the image itself.
QStandardItemModel
and Performance
Qt’s support for model-view-controller is great, and one of the handiest things about it is that the QStandardItemModel
class provides everything you need in a model to get going. It is, however, not necessarily the most efficient way to do so.
If you have your own model already—say you’re porting an existing application, or working with a library with its own data representation—it really is a good idea to implement your own model class from QAbstractItemModel
. I’m not bad-mouthing QStandardItemModel
, but it’s based on Qt’s collection classes, and odds are that you know better ways to store your data (especially if there’s a lot of it!) than Qt does.
Personally, my approach is that if I don’t have a data container and I’m only dealing with a handful of data items — say, up to a hundred or so objects with a half-dozen roles or so — I use QStandardItemModel
and don’t look back. (Qt’s collection classes are, after all, quite good for general applications). But if I’ve already got a data model, or I know I’m going to be dealing with lots and lots of objects, it’s time for a custom model that leverages my knowledge of the data set I’m wrapping.
Use QtConcurrent
for Parallel Computation
I’ve not tried this personally, but QtConcurrent
looks really cool for doing any kind of concurrent map/reduce style programming. If you need to write code that leverages multiple cores for concurrent computation, it’s best to look here before trying to roll your own framework using QThread
.