Create a QPaintDevice from HDC handle

2019-07-21 16:47发布

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?

标签: c++ qt hdc
1条回答
虎瘦雄心在
2楼-- · 2019-07-21 16:59

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".

查看更多
登录 后发表回答