Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to display path with QRegion member of Ellipse type correctly?



  • Hello everyone! I have few questions about QPainter, QRegion and Clipping.
    My goal is to dislay intersection of two ellipses. Fitst, QPainter draws ellipses correctly:
    void Field1::paintEvent(QPaintEvent *event) {
    QPainter pntr;
    QRect r1(50, 50, 100, 100);
    QRect r2(100, 50, 100, 100);
    pntr.begin(this);
    pntr.setBrush(Qt::yellow);
    pntr.drawEllipse(r1);
    pntr.drawEllipse(r2);
    pntr.end();
    }
    4cc81ec7-e0d5-4b51-9686-f163146a55d5-image.png
    Next I try to create two regions reg1, reg2 of type ellipse and to get region reg3 that is an intersection of reg1 and reg2. After that I add reg3 to QPainterPath p and draw it. The result contains an intersection with right shape but whith stripes of two colors yellow (brush color) and black. Why it isn't full yellow and contains those stripes?
    void Field1::paintEvent(QPaintEvent *event) {
    QPainter pntr;
    QRect r1(50, 50, 100, 100);
    QRect r2(100, 50, 100, 100);
    pntr.begin(this);
    pntr.setBrush(Qt::yellow);
    QRegion reg1(r1, QRegion::Ellipse);
    QRegion reg2(r2, QRegion::Ellipse);
    QRegion reg3 = reg1.intersected(reg2);
    QPainterPath p;
    p.addRegion(reg3);
    pntr.drawPath(p);
    pntr.end();
    }
    59a90bba-63ab-43f3-ad64-abed6157bbde-image.png
    What is wrong? If I try to get intersection of rectangles everything is ok:
    QRegion reg1(r1, QRegion::Rectangle);
    QRegion reg2(r2, QRegion::Rectangle);
    393fdfd3-8fa2-4bf2-8bb1-e49084f52b08-image.png
    Also I tried to use clipping, specifically functions setClipRegion and setClipPath with function update() and it has no effect, nothing is displayed
    pntr.setClipping(true);
    pntr.setClipPath(p);
    this->update();
    or
    pntr.setClipping(true);
    pntr.setClipRegion(reg3);
    this->update();

    It seems, that is not enough. But I didn't find in documentation more actions to do. How it should be? And the last question, expression pntr.setRenderHint(QPainter::Antialiasing, true); doesn't help to make the contour of shape smooth, it has no effect. What is wrong with it? Thanks.

    P.S. If i use setClipPath() or setClipRegion() and after that draw one of start rectangulars, result looks like the right one, but whatever it contains some black pixels at the right side. Is it normal? What is wrong??
    void Field1::paintEvent(QPaintEvent *event) {
    QPainter pntr;
    QRect r1(50, 50, 100, 100);
    QRect r2(100, 50, 100, 100);
    pntr.begin(this);
    pntr.setBrush(Qt::yellow);
    pntr.setClipping(true);
    QRegion reg1(r1, QRegion::Ellipse);
    QRegion reg2(r2, QRegion::Ellipse);
    QRegion reg3 = reg1.intersected(reg2);
    QPainterPath p;
    p.addRegion(reg3);
    pntr.setClipRegion(reg3);
    pntr.drawEllipse(r1);
    pntr.end();
    }
    69e85a4c-80e6-4a6b-8238-3a7d08c321aa-image.png



  • I understood what is wrong with path, intersected region consists of many rectangles, and they are circled with black pen. So to remove black stripes i added pntr.setPen(Qt::NoPen); and no more black lines. So variant with drawPath() works. Now, how to use setClipRegion() or setClipPath() with update()? It doesn't display anything.


Log in to reply