Qt + WINAPI + direct painting
I'm experimenting with direct painting to device contexts using WINAPI. Here's what my widget's paintEvent function looks like:
Code:
{
qDebug() << "Start paint function";
HDC hdc = this->getDC();
if (hdc == NULL)
{
qDebug() << "HDC error";
return;
}
qDebug() << "Start painting";
HPEN hPen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0));
if (hPen == NULL)
qDebug() << "CreatePen error";
if (SelectObject(hdc, hPen) == HGDI_ERROR)
qDebug() << "SelectObject error";
if (MoveToEx(hdc, 0, 0, NULL) == 0)
qDebug() << "MoveToEx error";
if (LineTo(hdc, 200, 200) == 0)
qDebug() << "LineTo error";
qDebug() << "End painting";
this->releaseDC(hdc);
qDebug() << "End paint function";
}
When I run the program, I don't see a solid red line appearing as I should. The console output seems correct:
Quote:
Start paint function
Start painting
End painting
End paint function
so it looks like everything in the function is getting called properly, but I don't see an output. It's been a while since I've done WINAPI stuff, but I think I have it right. Can anyone offer any insight?
Re: Qt + WINAPI + direct painting
What about setting flag Qt::WA_PaintOnScreen?
Re: Qt + WINAPI + direct painting
Quote:
Originally Posted by
minimoog
What about setting flag Qt::WA_PaintOnScreen?
I read in the docs this attribute is only supported under X11, and I'm running Windows.
Still, I tried it, no effect.
[edit]
I made a discovery. If I inherit from QWidget instead of QGraphicsView, it still doesn't work, but if I rapidly resize the widget I can see the line flickering, until it (presumably) gets painted over.
So what in QWidget is being called after paintEvent that's painting over my line?
Re: Qt + WINAPI + direct painting
Set Qt::WA_PaintOnScreen in the QWidget constructor. Reimplement QPaintDevice::paintEngine() to return a null pointer. Just like is written in documentation on Qt::WA_PaintOnScreen.
It should be working.
Re: Qt + WINAPI + direct painting
Why are you using WinAPI to paint? Is there a problem with QPainter?
Re: Qt + WINAPI + direct painting
I got it working, finally!
Quote:
Originally Posted by
minimoog
Set Qt::WA_PaintOnScreen in the QWidget constructor. Reimplement QPaintDevice::paintEngine() to return a null pointer. Just like is written in documentation on Qt::WA_PaintOnScreen.
It should be working.
That's what I did at first, but it didn't work. After you said this I went back to check it out and noticed something: when overriding paintEngine() I forgot to put the trailing "const" in the declaration and definition, so instead of
I had
which of course did nothing. So thanks!
Quote:
Originally Posted by
pherthyl
Why are you using WinAPI to paint? Is there a problem with QPainter?
To be honest, I never really considered QPainter. I was trying to do this as low-level as possible to reduce the overhead introduced by Qt's painting framework. But it looks like QPainter will do exactly what I need, thanks!
Re: Qt + WINAPI + direct painting
Quote:
But it looks like QPainter will do exactly what I need, thanks!
Thats how it is done in Qt.
Quote:
To be honest, I never really considered QPainter. I was trying to do this as low-level as possible to reduce the overhead introduced by Qt's painting framework.
Then why use Qt... there will be some overhead still :P
juzz kidding.. theres always tradeoff between complexity of code and runtime. And Qt's paint framework is not that slow..also if you use QPainter, your code will be cross platform !
Re: Qt + WINAPI + direct painting
Well I've been experimenting with QPainter and so far it's working very well!
Re: Qt + WINAPI + direct painting
Quote:
Originally Posted by
JovianGhost
Well I've been experimenting with QPainter and so far it's working very well!
Good to know :) ... I guess you had underestimated Qt's performance :P