title: Drawing by Windows GDI inside of Qt
body: <p>I am trying to use Windows GDI inside of QGraphicsView paintEvent but have noticed some drawing issues, for example the drawing disappear or blinking when I resize window or minimize and maximize. When I use Qt instead of GDI its working perfectly. Here is the code:</p>
  
  <p>[UPDATED CODE]</p>
  
  <pre><code>#include "CustomView.h"
  
  #include &lt;QPainter&gt;
  #include &lt;QPaintEngine&gt;
  #include &lt;windows.h&gt;
  #include &lt;objidl.h&gt;
  #include &lt;gdiplus.h&gt;
  using namespace Gdiplus;
  #pragma comment (lib,"Gdiplus.lib")
  
  CustomView::CustomView(QWidget *parent)
      : QGraphicsView(parent)
  {
      setAutoFillBackground(true);
      setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
      setAttribute(Qt::WA_NativeWindow, true);
  }
  
  CustomView::~CustomView()
  {
  
  }
  
  void CustomView::paintEvent(QPaintEvent * event)
  {
      QPainter painter(viewport());
      painter.beginNativePainting();
      // Drawing by Windows GDI
      HWND hwnd = (HWND)viewport()-&gt;winId();
      HDC hdc = GetDC(hwnd);
  
      QString text("Test GDI Paint");
      RECT rect;
      GetClientRect(hwnd, &amp;rect);
  
      HBRUSH hbrRed = CreateSolidBrush(RGB(0, 255, 0));
      FillRect(hdc, &amp;rect, hbrRed);
  
      Ellipse(hdc, 50, 50, rect.right - 100, rect.bottom - 100);
      SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
      TextOutW(hdc, width() / 2, height() / 2, (LPCWSTR)text.utf16(), text.size());
      ReleaseDC(hwnd, hdc);
      painter.endNativePainting();
  
      QGraphicsView::paintEvent(event);
  
      // Drawing the same by Qt
      //  QPainter painter(viewport());
      //  painter.save() ;
      //  QBrush GreenBrush(Qt::green);
      //  QBrush WhiteBrush(Qt::white);
      //  QRect ViewportRect = viewport()-&gt;rect();
      //  painter.fillRect(ViewportRect, GreenBrush);
      //  painter.drawEllipse(50, 50, ViewportRect.right() - 100, ViewportRect.bottom() - 100);
      //  QPainterPath EllipsePath;
      //  EllipsePath.addEllipse(50, 50, ViewportRect.right() - 100, ViewportRect.bottom() - 100);
      //  painter.fillPath(EllipsePath, WhiteBrush);
      //  painter.drawText(ViewportRect.width() / 2, ViewportRect.height() / 2, "Test Qt Paint");
      //  painter.restore();
  }
  </code></pre>
  
  <p>and here is the whole project(Visual Studio 2010 + Qt 5.4.1)[UPDATED ARCHIVE]
  <a href="https://dl.dropboxusercontent.com/u/105132532/Stackoverflow/Qt5TestApplication.7z" rel="nofollow">https://dl.dropboxusercontent.com/u/105132532/Stackoverflow/Qt5TestApplication.7z</a></p>
  
  <p>Any ideas?</p>
  
  <p>Solution(edited the code after Kuba Ober's answer)</p>
  
  <pre><code>#include "CustomView.h"
  
  #include &lt;QPainter&gt;
  #include &lt;QPaintEngine&gt;
  #include &lt;windows.h&gt;
  #include &lt;objidl.h&gt;
  #include &lt;gdiplus.h&gt;
  
  using namespace Gdiplus;
  #pragma comment (lib,"Gdiplus.lib")
  
  
  CustomView::CustomView(QWidget *parent)
      : QGraphicsView(parent)
  {
      // This brings the original paint engine alive.
      QGraphicsView::paintEngine();
      setAttribute(Qt::WA_NativeWindow);
      setAttribute(Qt::WA_PaintOnScreen);
      setRenderHint(QPainter::Antialiasing);
  }
  
  CustomView::~CustomView()
  {
  
  }
  
  QPaintEngine* CustomView::paintEngine() const
  {
      return NULL;
  }
  
  bool CustomView::event(QEvent * event) {
      if (event-&gt;type() == QEvent::Paint)
      {
          bool result = QGraphicsView::event(event);
          drawGDI();
          return result;
      }
      else if (event-&gt;type() == QEvent::UpdateRequest)
      {
          bool result = QGraphicsView::event(event);
          drawGDI();
          return result;
      }
      return QGraphicsView::event(event);
  }
  
  void CustomView::drawGDI()
  {
      // Drawing by Windows GDI
      HWND hwnd = (HWND)viewport()-&gt;winId();
      HDC hdc = GetDC(hwnd);
  
      QString text("Test GDI Paint");
      RECT rect;
      GetClientRect(hwnd, &amp;rect);
  
      HBRUSH hbrRed = CreateSolidBrush(RGB(0, 255, 0));
      FillRect(hdc, &amp;rect, hbrRed);
  
      Ellipse(hdc, 50, 50, rect.right - 100, rect.bottom - 100);
      SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
      TextOutW(hdc, width() / 2, height() / 2, (LPCWSTR)text.utf16(), text.size());
      ReleaseDC(hwnd, hdc);
  }
  </code></pre>