How to do this using QPainter?



  • Hi,

    I have widget filled by green color. Next I draw red ellipse. And I would like to draw transparent rectangle on the ellipse to see green background like this:

    app.png

    I know that I can use QPainterPath to draw what I want: using arc, lines etc.

    But maybe there is a simple solution to use only something like:

    painter->drawRext(100,100,10,10, Qt::transparent);
    

    EDIT

    Of course I know that i can draw green rectangle, but a green background is only example.



  • Hi,

    You can substract two paths:

           int radius=100;
           QPainterPath path;
           path.addEllipse(rect().center(),radius,radius);
           QPainterPath recPath;
           int recW=radius/2;
           QRectF rec=path.boundingRect();
           rec.setSize(QSize(recW,recW));
           rec.translate(0,radius-recW/2);
           recPath.addRect(rec);
           path=path.subtracted(recPath);   // substract rect from ellipse
           
           painter.fillPath(path,Qt::red);
    


  • Hi,

    You can substract two paths:

           int radius=100;
           QPainterPath path;
           path.addEllipse(rect().center(),radius,radius);
           QPainterPath recPath;
           int recW=radius/2;
           QRectF rec=path.boundingRect();
           rec.setSize(QSize(recW,recW));
           rec.translate(0,radius-recW/2);
           recPath.addRect(rec);
           path=path.subtracted(recPath);   // substract rect from ellipse
           
           painter.fillPath(path,Qt::red);
    

Log in to reply