QT: Scroll widget that renders directly to the DC
I'm trying to create a widget which开发者_运维百科 paints directly to the windows Device Context by calling getDC()
and painting an HBITMAP
to it.
paintEvent()
and it does seem to paint but immediatly after painting the widget gets painted over again with a blank gray color.
I've tried setting WA_PaintOnScreen
and Qt::WA_NoSystemBackground
but none of those help.
GLWidget
works.
What am I missing?
Found the answer here:
http://www.qtchina.net/qt4c++guiprogramming/ch20lev1sec1.html/
void GdiControl::paintEvent(QPaintEvent * /* event */)
{
RECT rect;
GetClientRect(winId(), &rect);
HDC hdc = GetDC(winId());
FillRect(hdc, &rect, HBRUSH(COLOR_WINDOW + 1));
SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
TextOutW(hdc, width() / 2, height() / 2, text.utf16(), text.size());
ReleaseDC(winId(), hdc);
}
For this to work, we must also reimplement
QPaintDevice::paintEngine()
to return a null pointer and set theQt::WA_PaintOnScreen
attribute in the widget's constructor.
精彩评论