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

Prevent QQuickPaintedItem from clipping



  • I am trying to get a custom QQuickPaintedItem subclass to repaint outside the bounds. I have tried three separate ways to turn off clipping, but nothing works:

    • setClip(false)
    • setFlag(QQuickItem::ItemClipsChildrenToShape, false)
    • clip: false

    Here is the screenshot of the clipped paint path.
    0_1545834468146_Screen Shot 2018-12-26 at 9.27.32 AM.png

    I would expect the final result to be a triangle, but as can be seen the QQuickPaintedItem's bounds are clipping the path.

    Unfortunately because I am embedding this into a MapQuickItem, it is not really an option to simply grow the canvas to fill the parent.

    What am I misunderstanding about disabling clipping?

    MINIMAL EXAMPLE

    Below is the full example required to reproduce the above graphic.


    paint_clipping_example.cpp

    #include "paint_clipping_example.h"
    #include <QPainter>
    
    ClipExample::ClipExample(QQuickItem *parent)
        : QQuickPaintedItem(parent)
    {
        setClip(false);
        setFlag(QQuickItem::ItemClipsChildrenToShape, false);
    }
    
    void ClipExample::paint(QPainter *painter)
    {
        painter->setClipping(false);
        painter->setPen(QPen(QBrush(QColor("red")), 3.0));
    
        painter->drawLine(-100, 10, 20, 0);
    
        painter->setPen(QPen(QBrush(QColor("blue")), 3.0));
    
        painter->drawLine(20, 0, 100, 50);
    
        painter->setPen(QPen(QBrush(QColor("magenta")), 3.0));
    
        painter->drawLine(100, 50, -100, 10);
    }
    

    paint_clipping_example.h

    #ifndef PAINT_CLIPPING_EXAMPLE_H
    #define PAINT_CLIPPING_EXAMPLE_H
    
    #include <QtQuick/QQuickPaintedItem>
    #include <QColor>
    #include <QtQuick>
    
    class ClipExample : public QQuickPaintedItem
    {
        Q_OBJECT
    
    public:
        ClipExample(QQuickItem *parent = nullptr);
        void paint(QPainter *painter);
    };
    
    #endif
    

    app.qml

    import ClipExample 1.0
    import QtQuick 2.0
    
    Rectangle {
        width: 200; height: 100
    
        ClipExample {
            x: 100
            y: 40
    
            width: 40
            height: 50
    
            clip: false
        }
    }
    

    main.cpp

    #include "paint_clipping_example.h"
    #include <QtQuick/QQuickView>
    #include <QGuiApplication>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<ClipExample>("ClipExample", 1, 0, "ClipExample");
    
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        view.setSource(QUrl("qrc:///app.qml"));
        view.show();
        return app.exec();
    }
    

    example.pro

    QT += qml quick
    
    HEADERS += paint_clipping_example.h
    SOURCES += paint_clipping_example.cpp \
               main.cpp
    
    RESOURCES += paint_clipping_example.qrc
    

    paint_clipping_example.qrc

    <RCC>
        <qresource prefix="/">
            <file>app.qml</file>
        </qresource>
    </RCC>
    

Log in to reply