title: Qt- How to get the color of a QPoint in QImage for QPixmap
body: <p>I am trying to implement the flood-fill algorithm, which requires on each iteration to get the color of point in an area. But the color of the selected point is always the background color even if I have drawn something.</p>
  
  <pre><code>void MainWindow::floodFill(QPainter&amp; p, int x, int y, const QColor&amp; cu, const QColor&amp; cc) {
  
     st.push(QPair&lt;int, int&gt;(x, y)); 
  
     //used for debug
     int _r, _g, _b;
     int _rCu, _gCu, _bCu;
     int _rCc, _gCc, _bCc; 
     cu.getRgb(&amp;_rCu, &amp;_gCu, &amp;_bCu);
     cc.getRgb(&amp;_rCc, &amp;_gCc, &amp;_bCc);
  
     while (!st.isEmpty()) {
        QPair&lt;int, int&gt; pair = st.pop();
        QPixmap qPix = ui-&gt;centralWidget-&gt;grab();
        QImage image(qPix.toImage());
        QColor c(image.pixel(pair.first, pair.second));
  
        //used for debug, here QColor c is always the same
        c.getRgb(&amp;_r, &amp;_g, &amp;_b);
  
        if (c == cu || c == cc) {
           continue;
        }
        p.setPen(cu);
        p.drawPoint(pair.first, pair.second);
  
  
        if (pair.first &gt; 0) {
           st.push(QPair&lt;int, int&gt;(pair.first - 1, pair.second));
        }
        if (pair.first &lt; 200/*ui-&gt;centralWidget-&gt;width()*/) {
           st.push(QPair&lt;int, int&gt;(pair.first + 1, pair.second));
        }
        if (pair.second &gt; 0) {
           st.push(QPair&lt;int, int&gt;(pair.first, pair.second - 1));
        }
        if (pair.second &lt; 200/*ui-&gt;centralWidget-&gt;height()*/) {
           st.push(QPair&lt;int, int&gt;(pair.first, pair.second + 1));
        }
     }
  }
  </code></pre>
  
  <p>this is what i call in paint event</p>
  
  <pre><code>void MainWindow::paintEvent(QPaintEvent* event) {
     QPainter p(this);
     QColor colorRed(255, 0, 0);
     QColor colorBlack(0, 0, 0);
  
     p.setPen(QPen(colorBlack));
  
     p.drawRect(50, 50, 3, 3);
  
  
     floodFill(p, 51, 51, colorRed, colorBlack);
  }
  </code></pre>