Search code examples
qtqmlselectionimage-editing

How select an area in QQuickPaintedItem


I want to build a selection tool for my qml image editor.

For this, I'm looking for an similar function like setSelectedArea in a QGraphicsScene. Has someone a solution for this?

Greetings

Edit: Maybe I can write a Plugin for my selection tool which extends the QQuickItem and draw a QPolygon with openGL.


Solution

  • You need to implement selection yourself.

    You can create MouseArea that will track mouse activity and update selected rect accordingly. I mean something like this:

    DocumentViewer { // Your QQuickPaintedItem
        id: viewer
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton
            property real originX: 0
            property real originY: 0
            onPressed: {
                originX = mouse.x
                originY = mouse.y
            }
            onPositionChanged: {
                var width = mouse.x - originX
                var height = mouse.y - originY
                viewer.selectionRect = Qt.rect(originX, originY, width, height)
            }
        }
    }
    

    Then you'll be able to update and paint the selection rectangle in your viewer's selectionRect property setter.