我必须在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()
}
}
}