如何在 PyQT 中绘制节点和边?
在 PyQT 中,如何在给定点绘制小“节点”并将它们与边缘连接?我找到的所有 PyQT 教程都是“绘制一个按钮!绘制一个复选框!”
In PyQT, how can I plot small "Nodes" at given points and connect them with edges? All of the PyQT tutorials I find are "plot a button! plot a checkbox!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此找到一个好的解释是一件很痛苦的事情(截至 2014 年底已经),并且由于这个问题准确地询问了我正在寻找的内容,因此我将发布我的内容的转录(从 C++ 到 Python)可以在这篇文章中找到。
代码如下,其基本原理如下:
QGrahpicsItem
、QPainterPath
和QPainterPath.Element
是您要查找的类。具体来说, QPainterPath 实现了您期望在应用程序中使用的矢量功能,例如CorelDraw、Adobe Illustrator 或 Inkscape。QGraphicsEllipseItem
(用于渲染节点)和QGraphicsPathItem
(用于渲染路径本身),它们继承自QGraphicsItem
。Path
构造函数迭代QPainterPath
元素,为每个元素创建Node
项;它们中的每一个依次将更新发送到父 Path 对象,从而相应地更新其path
属性。It has been a pain to find a good explanation for this (as of by the end of 2014 already), and since this question asks exactely what I was looking for, I'll post a transcription (from C++ to Python) of what I found in this post.
The code is below, and here is the rationale:
QGrahpicsItem
,QPainterPath
andQPainterPath.Element
are the classes you are looking for. Specifically, QPainterPath implements the kind of vector functionality you expect in applications such as CorelDraw, Adobe Illustrator, or Inkscape.QGraphicsEllipseItem
(for rendering nodes) andQGraphicsPathItem
(for rendering the path itself), which inherit fromQGraphicsItem
.Path
constructor iterates over theQPainterPath
elements, creatingNode
items for each one; Each of them, in turn, send updates to the parent Path object, which updates itspath
property accordingly.如果您希望能够与图中显示的对象进行交互,最好使用 QGraphicsScene。它处理缩放和平移,并且可以包含其他可以处理自己的交互的 QGraphicsItem 对象。
它非常容易使用,但会涉及一些开销,特别是如果您计划制作数千个对象。
您可以在此处找到 PyQt 教程。这和 API 文档应该可以帮助您入门。
If you want to be able to interact with the objects displayed in the plot, you will be better off using a QGraphicsScene. It handles zooming and panning and can contain other QGraphicsItem objects that can handle their own interactions.
It's very easy to use, but there is a bit of overhead involved, especially if you plan to make thousands of objects.
You can find a PyQt tutorial here. This and the API docs should get you started.