1

目前有一个程序,用户可以使用鼠标点击绘制自己的形状。然后它获取一个 QPointF 列表(通过用户鼠标点击获得)并创建一个 QPolygonF。这一步很好,当转换为 PolygonItem 并添加到图形视图时,可以看到多边形的可见轮廓。但是我需要将其转换为 QImage 并将其像素设置为绿色(稍后解决质心时颜色很重要),但我不确定该怎么做。目前我有...

self.poly=QPolygonF(self.vertexList) #works

self.polyItem=QGraphicsPolygonItem(self.poly) #works, visible when added to scene

self.pixItem=QGraphicsPixmapItem(self.polyItem) #for some reason this step doesn't work. The pixmap item is created, however its empty

self.addItem(self.pixItem) #not visible, this is where it goes wrong

self.pix = self.pixItem.pixmap()

self.image=self.pix.toImage()

self.image.convertToFormat(4)#4=RGB32

value=qRgb(0,255,0) #These last two lines turn it green but irrelevant to issue

self.image.fill(value)

据我了解,您可以将一种类型的项目转换为另一种类型(没有给出错误),但是将多边形项目转换为像素图项目时它会变为空白。将不胜感激任何帮助。

4

0 回答 0