如何在QGraphicsItem中绘制一半的饼图
我试图在 qGraphicsview 框架中绘制这样的一半饼图。
我尝试绘制两个饼图其中一个的颜色是透明的,但它不起作用,我决定在这里询问。
因为不太了解QPainterPath,所以画圆弧和饼图没成功!。
任何答案或想法将不胜感激。
I'm trying to draw half of pie like this in qGraphicsview framework.
And I tried to draw two pie and the color of one of them is transparent but it didn't work and i decided to ask here.
Because I dont know QPainterPath very well, I was unsuccessful to draw arc and pie!.
Any answer oridea will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意 Neox 的观点,这张图片并不是真正的馅饼;)。如果你想要一个实际的饼图(即:圆的一部分),你可以使用画家的内置方法:
int radius
显然,你的馅饼的半径。
int startAngle
饼图开始的位置,例如与正 X 轴偏移 20°
int span
弧延伸的度数,例如,跨度 = 320° 的饼图将在距正 X 轴总共 340° 或 -20° 处结束。 (有关神奇的 *16,请参阅 QPainter 文档)
这应该会给您一个漂亮的 PacMan ^^ - 或者您可以尝试使用这些值。
现在,在没有更多详细信息的情况下,您需要弄清楚该片段的实际放置位置。当然,它会在某个图形项(例如自定义 QGraphicsItem)的 Paint 方法中,因此使用作为此方法的输入的画家。这实际上取决于您对小部件和其他内容的设置...
Qt 实际上有一个非常棒的文档,因此请务必先检查一下:(我自己只花了两周的时间,但是您可以通过以下方式获得很大的帮助)文档)
QGraphicsItem:http://qt-project.org/doc/qt-4.8/qgraphicsitem.html
QPainter: http://qt-project.org/doc/qt-4.8/qpainter .html
干杯,
路易丝
PS:第一个答案,耶!
I agree with Neox, the picture is not really a pie ;). If you want an actual pie (that is: a segment of a circle) you can use the inbuilt method of painter:
int radius
the radius of your pie, obviously.
int startAngle
where your pie starts, e.g. 20° offset from the positive X axis
int span
number of degrees that the arc stretches, e.g. a pie with span = 320° would end at a total of 340°, or -20° from the positive X axis. (see QPainter Documentation for the miraculous *16)
This should give you a nice PacMan ^^ - or you can play around with the values.
Now, without any more details you need to figure out where to actually place this snippet. Naturally, it would be in the paint method of a some graphical item, e.g. a custom QGraphicsItem, and hence use the painter that comes as input to this method. This really depends on your setup of widgets and stuff...
Qt has actually a pretty awesome documentation, so make sure to check this out first: (I've only been at it for two weeks myself, but you can get pretty far with the documentation)
QGraphicsItem: http://qt-project.org/doc/qt-4.8/qgraphicsitem.html
QPainter: http://qt-project.org/doc/qt-4.8/qpainter.html
Cheers,
Louise
PS: First answer, yay!
擦除并不实际,因为很快您就会擦除图像中的其他内容,或者至少您需要非常小心绘制内容的顺序。
从字面上看,如果您正在寻找一种绘制饼图的方法,drawPie() 方法可以做到这一点,但是查看您提供的示例图像,您会寻找类似饼图的垫圈部分。
像这样的东西:
这是使用如下代码生成的:
上面授予的内容不适用于 QGraphicsItem。但是我相信您应该执行此 QGraphicsPathItem 并从上面提取代码来创建路径。
我不想发布使用 QGraphicsPathItem 的代码,因为我没有时间测试该代码,但我相信上面的代码片段首先解决了如何创建几何图形的基本困难。
Erasing is not practical because very soon you will be erasing other stuff in image or at least you need to be very careful in which order you draw things.
Literally, if you are looking for a way to draw a pie, the drawPie() method does it but looking at the example image you gave you looking for a pie like segment of a washer.
Something like this:
This was produced with code like this:
Granted above does not do it with QGraphicsItem. However I believe you should be doing this QGraphicsPathItem and lift the code from above to create the path.
I did not want to post code that uses QGraphicsPathItem because I do not have time to test that code but I believe above code snippet solves the essential difficulty of how to create the geometry in the first place.