0

I am extracting thumbnails of files from Windows in a secondary thread and the output is an HBITMAP. Now I got to convert this to QImage to send it back to the main thread. As you might already know, using QPixmap in non-gui threads results in runtime errors and a compile-time warning. I know because I did try to use it, and it gave me random errors.

Any ideas would be appreciated.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Prad Lal
  • 113
  • 7

1 Answers1

0

When extracting the thumbnails in the secondary thread, why not just store the HBitmaps until you are back in the main thread again?

So just use a collection of HBitmaps as your container instead of QPixmaps...

(Reading through the documentation, it doesn't look like there is an easy way to accomplish the task without using QPixmaps).

An added option (if the above route proves to be too slow at the time of rendering) is to have your main thread periodically check your HBitmap list and do the conversion in kind of a Producer Consumer model where your secondary thread creates a short list of HBitmaps, and your main thread on a timer, goes and converts them. This should only be necessary if you are looking at a lot of thumbnails...

A third way that you could go about this, is go outside of Qt, and find another way to cache those bitmaps or an intermediate conversion that you could turn into a QImage. You could even go to the source of Qt and see how fromWinHBITMAP() is implemented and create your own.

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks @phyatt. I'll try bringing the HBITMAP to the main thread option today (I did try it before posting the q, but somehow the HBITMAP was getting corrupted, I shall try it again). If it doesn't work, I shall go to the QPixmap source. Thanks for your thoughts. – Prad Lal Mar 19 '13 at 01:12
  • HBitmap is a HANDLE, so if its source gets closed, you may have a "Closed" HANDLE on your hands. http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx – phyatt Mar 19 '13 at 03:42
  • I just implemented it with your first suggestion and it works great. The Producer sending the HBITMAP back to the consumer, instead of converting it to QPixmap in the producer thread. And the error I had was because of a typo in registering the MetaType, not a very pretty error. For anyone who might read this in future, I did look at the source of QPixmap::fromWinHBITMAP(), it uses GetDBIBits() which require a device context, which makes it dependent on the graphic subsystem. ie cannot be used in a non-gui thread. Cheers :) – Prad Lal Mar 19 '13 at 05:58