Is QPixmap reentrant?

2019-09-11 07:52发布

问题:

I have a program that needs to load a lot of QPixmaps. I split the loading of the pixmaps in several jobs using QtConcurrent::mappedReduced (I actually load a bunch of QGraphicPixmapItems). The loading function calls only the constructors of the QPixmaps/QGraphicItems, it does not attempt to perform any drawing, and it does not communicate with the rest of the world (at least through my code) until the loading is finished.

I get some random crashes during the initialization (say 1% of the times), and helgrind complains about unguarded accesses to QApplication from the QPixmap and from the main event loop, but it is known that Qt mutexes generally do not mix well with valgrind, so it might be a false positive.

As usual, the Qt documentation is quite unclear on whether QPixmap is reentrant or not, that is basically my question.

回答1:

Well, you get crashes and you ask if it's OK? You already know the answer. It's not OK.

The only question I see here is whether it's a Qt bug. No, it's not.

If you want to load a lot of pixmaps, load them in into QImages, and then convert them into the backing store format. There isn't all that much to be gained these days from using a pixmap over an image. As long as the image has the same format as the widget's backing store (cast to QImage), you'll have same performance. The QPixmap distinction made sense when Qt still used native painting. On Windows and OS X, a pixmap is just a properly formatted QImage.