Create a QPaintDevice from HDC handle

2019-07-21 17:07发布

问题:

I have a Windows HDC handle from an external library that I'd like to use QPainter functionality to draw on. Is there any way in Qt to create a QPaintDevice from a HDC handle?

回答1:

One way of doing this:

Using the Windows API, get the HWND from the HDC.

HWND handle = WindowFromDC(hdc);
assert(handle != NULL);

Then subclass QWidget to get access to the protected member convert. Using this, create the QWidget using this member as described in this solution: How to create a qwidget with a hwnd as parent. In this example, I've called the subclass for QWidgetWrapper.

QWidgetWrapper *w = new QWidgetWrapper();
w->create((Wld)main_window);

Note that Wld is a typedef in Qt for "Platform dependent window identifier".



标签: c++ qt hdc