Can I Get hWnd or DC from QPainter in QGraphicsPixmapItem's paint() when I just need coding in Windows?
Can you help to let me get hWnd or DC from QPainter in QGraphicsPixmapItem's paint()?
I am just coding in Windows. I need to add some features which need hWnd and DC to draw in my object.
Should I need to change the src of QPainter and re-compile QT5?
in your paintEvent() handler you also get a QWidget parameter.
On that call QWidget::effectiveWinId() and you will get the HWND.
Thanks. I do get the hWnd or DC in paint() of subclass of QGraphicsPixmapItem, but what I draw is covered, I cannot see it, how to draw my object with others those using QPainter?
The only chance I can see what I draw is when I maximize the app, it flashed in about 0.1 sec then covered by the backgroud of the scene.
Can you give me some advice?
are you calling QGraphicsPixmapItem::paint(...) (the base class implementation) somewhere? anyway, if you need to have the default painting plus some extra ones just call it at the beginning of your paint(...) implementation
no, I am not calling the base class's paint(), and the draw using QWidget::effectiveWinId() is at the beginning of my paint().
I guess the problem is caused by the painting mechanism of the Qt scene. All the paints of scene and its object using QPainter are drawn in a memory DC then drawn to the window of QWidget::effectiveWinId()? Can you tell me the truth?
I have solved the problem by myself:
In Qt5, QPaintEngineEx is subclass of QPaintEngine, and QRasterPaintEngine is subclass of QPaintEngineEx, so I can:
QPaintEngine PE = painter->paintEngine();
QRasterPaintEngine * pRasterPE = dynamic_cast<QRasterPaintEngine > (PE);
HDC rasperDC = pRasterPE->getDC();
drawing in rasperDC is not covered by the backgroud of the scene.
I have just tested and effectiveWinId() is ok; it returns the HWND of that QWidget from paint event which is the same! as the effectiveWinId() of the graphics view used
@WId id = widget->effectiveWinId();
WId id1 = scene()->views().at(0)->effectiveWinId();
HDC dc = ::GetDC(id);
this code in paint event just works ...
be carefully as I said the device context belongs to the QGraphicsView(inheriting QWidget) used by the scene!