QPainter trick .



  • Hi Qt Community ,

    I tried countless times to create a certain shape but i failed :

    I want to draw a "donut"-like shape whose center is transparent , so i tried something like this , but it was in vain :

    painter->setBrush(MyColor);
    painter->drawEllipse(QPointF(0,0),10,10);
    painter->setBrush(Qt::NoBrush);
    painter->drawEllipse(QPointF(0,0),6,6);
    

    Any ideas ?



  • @Walux said:

    Any ideas ?

        QPainterPath path;
        path.addEllipse(0, 0, 400, 400);
        path.addEllipse(50,50,300,300);
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setBrush( QBrush(QColor("black")) );
        painter->drawPath(path);
    


  • @Wieland

    Works like a charm !
    I wonder why this worked !



  • @Walux said:

    I wonder why this worked

    See QPainterPath Class, especially note:

    Finally we draw the entire path. The path is filled using the default fill rule, Qt::OddEvenFill.

    Qt::OddEvenFill

    Specifies that the region is filled using the odd even fill rule. With this rule, we determine whether a point is inside the shape by using the following method. Draw a horizontal line from the point to a location outside the shape, and count the number of intersections. If the number of intersections is an odd number, the point is inside the shape. This mode is the default.



  • @Wieland

    Very informative :)
    U have my precious thanks ;)



  • @Walux :-)


Log in to reply