Getting a HBITMAP from a QPixmap in QT5 (Windows)

核能气质少年 提交于 2019-12-05 01:08:07
Gato

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!