来自Python中Numpy数组的画布上的多边形(QML)

我必须在Canvas上打印多边形,但是我不知道该怎么做。多边形的顶点存储在self .__ polygons中,该多边形是由另一个函数修改的numpy数组。 我尝试了几种方法,但是什么也没有。我已经注释了解释每个功能的代码。

GG.py

    class GG(QObject):

        def __init__(self,cameraIntrinsics,floorExtrinsics):
            QObject.__init__(self)
            self.__polygons = np.array

        def modArray(self):

            #function that elaborate 
            #polygon=[[a,b],[c,d],[e,f],[g,h]],where each [x,y]         
            #contains the coordinate of a vertice of the polygon,#are the vertices of the polygon,#and add the latter to
            #self.__polygons,that is an array of poligons


        def drawPol(self):
            #function that draw a polygon on canvas


    if __name__ == '__main__':
        app = QGuiApplication(sys.argv)
        gg = GG()
        engine = qqmlApplicationEngine()
        engine.rootContext().setContextProperty("gg",gg)

        currentPath = os.path.abspath(os.path.dirname(__file__))
        qmlPath = os.path.join(currentPath,'try.qml')
        engine.load(QUrl.fromLocalFile(qmlPath))  

        if not engine.rootObjects():
            sys.exit(-1)   

        sys.exit(app.exec_())

try.qml

    import QtMultimedia 5.0
    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtQuick.Controls.Material 2.12
    import QtQuick.Window 2.12
    import QtQml 2.11
    import Qt.labs.folderlistmodel 2.1
    import QtQuick.Dialogs 1.3
    import QtQml.StateMachine 1.0 as DSM
    import QtCharts 2.13

    ApplicationWindow {
        title: "window"
        visible: true
        id: window
        width:940
        height:510

        Rectangle {
            id: imgVisualizer
            anchors.left: window.left; anchors.top: window.top
            color: "transparent"
            width:610
            height:510
            Image {
                id: img
                anchors.fill: parent
                source: "file:///input.png"
            }

            Canvas {
                id: drawingCanvas
                anchors.fill: parent

                onPaint: {
                    var ctx = getcontext("2d")

                    ctx.setTransform()
                    ctx.lineWidth = 5;
                    ctx.strokeStyle = "red"

                    //print each edge of the polygon

                    ctx.beginPath()
                    ctx.moveTo(a,b)
                    ctx.lineTo(c,d)
                    ctx.lineTo(e,f)
                    ctx.lineTo(g,h)
                    ctx.stroke()
                }
          }
      }
didy21dd 回答:来自Python中Numpy数组的画布上的多边形(QML)

您必须注意以下事项:

  • 如果要与QML进行交互,建议使用QML本身接受的类,例如,不宜直接使用numpy数组,而应将其转换为例如QPointF数组。

  • 绘画不应在python中完成,而应在QML中完成,为此,您必须创建一个通知更改的属性,例如,创建一个显示列表的属性。

考虑到上述情况,解决方案是:

import os
import sys

import numpy as np

from PySide2.QtCore import Property,QObject,QPointF,QUrl,Signal
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine


class GG(QObject):
    polygonsChanged = Signal()

    def __init__(self,parent=None):
        super().__init__(parent)
        self._polygons = []

    def get_polygons(self):
        return self._polygons

    def set_polygons(self,polygons):
        self._polygons = polygons
        self.polygonsChanged.emit()

    polygons = Property(
        "QVariantList",fget=get_polygons,fset=set_polygons,notify=polygonsChanged
    )


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    gg = GG()

    numpy_arrays = np.array(
        [[[100,100],[150,200],[50,300]],[[50,60],[160,10],[400,0]]]
    )

    polygons = []
    for ps in numpy_arrays:
        polygon = []
        for p in ps:
            e = QPointF(*p)
            polygon.append(e)
        polygons.append(polygon)

    gg.polygons = polygons
    engine = QQmlApplicationEngine()
    engine.rootContext().setContextProperty("gg",gg)

    currentPath = os.path.abspath(os.path.dirname(__file__))
    qmlPath = os.path.join(currentPath,"try.qml")
    engine.load(QUrl.fromLocalFile(qmlPath))

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())
import QtQuick 2.12
import QtQuick.Controls 2.12

ApplicationWindow {
    id: window
    title: "window"
    visible: true
    width:940
    height:510

    Canvas {
        id: drawingCanvas
        anchors.fill: parent
        onPaint: {
            var ctx = getContext("2d")
            ctx.lineWidth = 5;
            ctx.strokeStyle = "red"
            for(var i in gg.polygons){
                var polygon = gg.polygons[i]
                ctx.beginPath()
                for(var j in polygon){
                    var p = polygon[j]
                    if(j == 0)
                        ctx.moveTo(p.x,p.y)
                    else
                        ctx.lineTo(p.x,p.y)
                }
                ctx.closePath()
                ctx.stroke()
            }
        }
    }
    Connections{
        target: gg
        onPolygonsChanged: drawingCanvas.requestPaint()
    }
}
本文链接:https://www.f2er.com/3159052.html

大家都在问