2

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?

dagur
  • 121
  • 1
  • 8
  • 1
    As far as I can see, it's not possible. QWidget (which inherits from QPaintDevice) has a concept of HDC. One can get its HDC through QWidget::getDC(), but one can not set it. Internally it's stored in QWidgetPrivate::dc; in the Pimpl class of QWidget and completely unaccessible. – dagur Apr 29 '12 at 22:05

1 Answers1

0

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

Community
  • 1
  • 1
dagur
  • 121
  • 1
  • 8
  • And why do you think there's a HWND associated with HDC? Have you heard about ["memory DC"](http://www.codeproject.com/Articles/224754/Guide-to-Win32-Memory-DC)? It has HDC, but no window. – SigTerm Apr 29 '12 at 23:47
  • You're right, thus the assert. This can only work if you can assume that it's not a memory DC. It's a bad solution, but the only one I've come up with. – dagur Apr 30 '12 at 00:17