#include #include #include #include #define USE_NATIVE_PAINTING #ifdef USE_NATIVE_PAINTING class DavidWidget : public QWidget { public: DavidWidget() : QWidget(0) { setAttribute(Qt::WA_PaintOnScreen, true); // the next three lines are useless //setAttribute(Qt::WA_NoSystemBackground, true); //setAttribute(Qt::WA_OpaquePaintEvent, true); //setAutoFillBackground(false); setGeometry(200, 200, 200, 200); } protected: void paintEvent(QPaintEvent *) override { // simulate long painting Sleep(500); HWND hwnd = (HWND)winId(); HDC hdc = GetDC(hwnd); QString text("Test GDI Paint"); RECT rect; GetClientRect(hwnd, &rect); HBRUSH hbrRed = CreateSolidBrush(RGB(255, 0, 0)); FillRect(hdc, &rect, hbrRed); HBRUSH hbrBlue = CreateSolidBrush(RGB(40, 40, 255)); HPEN bpenGreen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0)); SelectObject(hdc, bpenGreen); SelectObject(hdc, hbrBlue); Ellipse(hdc, 10, 10, rect.right - 20, rect.bottom - 20); SetTextAlign(hdc, TA_CENTER | TA_BASELINE); TextOutW(hdc, width() / 2, height() / 2, (LPCWSTR)(text.utf16()), text.size()); ReleaseDC(hwnd, hdc); } QPaintEngine *paintEngine() const override { return 0; } }; #endif class NormalWidget : public QWidget { public: NormalWidget() : QWidget(0) { setGeometry(450, 200, 200, 200); } protected: void paintEvent(QPaintEvent *) override { // simulate long painting Sleep(500); QPainter p(this); p.fillRect(rect(), Qt::blue); p.drawText(rect(), Qt::AlignCenter, "Normal widget"); } }; int main(int argc, char** argv) { QApplication app(argc, argv); #ifdef USE_NATIVE_PAINTING DavidWidget widget; widget.show(); #endif NormalWidget widget2; widget2.show(); return app.exec(); }