如何在 PyQT 中绘制节点和边? [英] How can I draw nodes and edges in PyQT?
本文介绍了如何在 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!"
推荐答案
为此找到一个好的解释一直很痛苦(截至 2014 年底已经),而且由于这个问题确切地问了我在看什么因为,我将发布我在 这篇文章.
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.
代码如下,原因如下:
QGrahpicsItem
、QPainterPath
和QPainterPath.Element
是您要查找的类.具体来说,QPainterPath 实现了您期望在诸如CorelDraw、Adobe Illustrator 或 Inkscape.- 下面的示例受益于预先存在的
QGraphicsEllipseItem
(用于渲染节点)和QGraphicsPathItem
(用于渲染路径本身),它们继承自QGraphicsItem
. Path
构造函数遍历QPainterPath
元素,为每个元素创建Node
项;它们中的每一个依次向父 Path 对象发送更新,该对象相应地更新其path
属性.- 我发现学习 C++ Qt4 文档比在别处找到的结构性较差的 PyQt 文档要容易得多.一旦您习惯了在 C++ 和 Python 之间进行心理转换,文档本身就是学习如何使用每个类的强大方法.
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.- The example below benefits from the pre-existing
QGraphicsEllipseItem
(for rendering nodes) andQGraphicsPathItem
(for rendering the path itself), which inherit fromQGraphicsItem
. - The
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. - I found much, much easier to study the C++ Qt4 Docs than the rather less structured PyQt docs found elsewhere. Once you get used to mentally translate between C++ and Python, the docs themselves are a powerful way to learn how to use each class.
<小时>
#!/usr/bin/env python
# coding: utf-8
from PyQt4.QtGui import *
from PyQt4.QtCore import *
rad = 5
class Node(QGraphicsEllipseItem):
def __init__(self, path, index):
super(Node, self).__init__(-rad, -rad, 2*rad, 2*rad)
self.rad = rad
self.path = path
self.index = index
self.setZValue(1)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
self.setBrush(Qt.green)
def itemChange(self, change, value):
if change == QGraphicsItem.ItemPositionChange:
self.path.updateElement(self.index, value.toPointF())
return QGraphicsEllipseItem.itemChange(self, change, value)
class Path(QGraphicsPathItem):
def __init__(self, path, scene):
super(Path, self).__init__(path)
for i in xrange(path.elementCount()):
node = Node(self, i)
node.setPos(QPointF(path.elementAt(i)))
scene.addItem(node)
self.setPen(QPen(Qt.red, 1.75))
def updateElement(self, index, pos):
path.setElementPositionAt(index, pos.x(), pos.y())
self.setPath(path)
if __name__ == "__main__":
app = QApplication([])
path = QPainterPath()
path.moveTo(0,0)
path.cubicTo(-30, 70, 35, 115, 100, 100);
path.lineTo(200, 100);
path.cubicTo(200, 30, 150, -35, 60, -30);
scene = QGraphicsScene()
scene.addItem(Path(path, scene))
view = QGraphicsView(scene)
view.setRenderHint(QPainter.Antialiasing)
view.resize(600, 400)
view.show()
app.exec_()
这篇关于如何在 PyQT 中绘制节点和边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文