我如何在PyQT中绘制节点和边? [英] How can I draw nodes and edges in PyQT?
问题描述
提前非常感谢
找到一个很好的解释是件很痛苦的事情(截至2014年底),而且由于这个问题要求我精确地寻找什么,我会发布我在这篇文章中找到的内容(从C ++到Python) / b> 代码如下,这里是基本原理:
QGrahpicsItem
, QPainterPath
和 QPainterPath.Element
是你正在寻找的课程。具体来说, QPainterPath 实现了您在应用程序中所需的矢量功能类型,例如CorelDraw,Adobe Illustrator或Inkscape。
QGraphicsEllipseItem
(用于渲染节点)和 QGraphicsPathItem
(用于渲染路径本身),它继承自 QGraphicsItem
。
路径
构造函数迭代 QPainterPath
元素,创建 Node
项为每一个;反过来,它们中的每一个都将更新发送到父路径对象,相应地更新其路径
属性。
#!/ usr / bin / env python
#编码:utf-8
来自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 =路径
self.index =索引
self.setZValue(1)
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
self.setBrush(Qt.green)
$ b $ def itemChange(self,change,value):
if change == QGraphicsItem.ItemPositionChange:
self.path.updateElement( self.index,value.toPointF())
return QGraphicsEllipseItem.itemChange( self,path,scene)
class Path(QGraphicsPathItem):
def __init __(self,path,scene):
super(Path,self).__ init__ (path)
for xrange(path.elementCount()):
node = Node(self,i)
node.setPos(QPointF(path.elementAt(i)))
scene.addItem(节点)
self.setPen(QPen(Qt.red,1.75))
def updateElement(self,index,pos):
path。 setElementPositionAt(index,pos.x(),pos.y())
self.setPath(path)
$ b if 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.Alliiasing)
view.resize(600,400)
view.show()
app.exec_()
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!"
Huge thanks in advance
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.- 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屋!