12

Now that QPixmap::toWinHBITMAP() has been deprecated, I can't find a way to get an HBITMAP from a QPixmap (or QImage).

Googling, I found there's a function called qt_pixmapToWinHBITMAP() which seems would do what I need, but I can't find what module I should enable -if any- in my .pro file or what header I should include to use it, or perhaps something else.

The reason I need a HBITMAP is to create a video using VFW. Of course, I'd love to be able to do that using only Qt. There's the QtMultimedia module, but as far as I can tell it doesn't export video, so I guess I'm stuck with using the windows api directly.

Any help would be appreciated.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Gato
  • 671
  • 5
  • 15
  • Hi, did you find any alternative? I have just upgraded to Qt5 and facing the same issue with my 'fromWinHICON' and 'fromWinHBITMAP' calls.. – Dalamber Sep 15 '13 at 11:24

3 Answers3

12

I found it!

All I needed was QtWinExtras

http://qt.gitorious.org/qt/qtwinextras

My code now looks something like this:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <qt_windows.h>
#include <QtWinExtras/QWinFunctions>


...
QPixmap pix(QSize(w,h));
...
HBITMAP hbm = QWinExtras::toHBITMAP(pix);
...
::DeleteObject(hbm);

I don't know if I need to include all those headers, but it works for me.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gato
  • 671
  • 5
  • 15
5

QtWinExtras is not necessary. Just use qt_pixmapToWinHBITMAP(), as QtWinExtras does.

Declare it soon after your includes:

QT_BEGIN_NAMESPACE
Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(
    const QPixmap &p, int hbitmapFormat = 0);

and just use it. For example, if you want to retrieve it from the QRC resources:

QPixmap pixmap(":/image.bmp");
HBITMAP hBitmap = qt_pixmapToWinHBITMAP(pixmap);
user3619296
  • 1,607
  • 1
  • 11
  • 3
0

In higher versions of Qt5, some functions and methods were removed or replaced, so solutions above should have a bit modification.My Qt version is Qt5.15.2, firstly, we should include QtWinExtras module, and some functions we may use were moved from namespace QtWinExtras to namespace QtWin.The code below can work well:

QPixmap map;
        
HICON icon = QtWin::toHICON(map);

Please note that after Qt6, almost all the extra modules are removed from Qt development kit, like Qt5WinExtras, QtQuick Extras and so on. So you can't use this method when you are using Qt6.