Qt: QPainter + GDI in the same widget?

前端 未结 2 1682
终归单人心
终归单人心 2021-01-24 15:32

I\'m trying to use the method described here to use a QPainter and GDI calls on the same widget.
Unfortunately this tutorial seem to have been written on an earlier version

2条回答
  •  攒了一身酷
    2021-01-24 16:07

    I got this working in QT 4.7-beta 2 as follows

    1. In the constructor call setAttribute(Qt::WA_PaintOnScreen,true);
    2. Do NOT reimplement paintEngine() to return NULL;
    3. Use the following code in the paintEvent();

      QPainter painter(this);
      HDC hdc = painter.paintEngine()->getDC();   // THIS IS THE CRITICAL STEP! 
      HWND hwnd = winID();
      
         // From this point on it is all regular GDI 
      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, text.utf16(), text.size());
      ReleaseDC(hwnd, hdc);
      

提交回复
热议问题