我如何在PyQT中绘制节点和边? [英] How can I draw nodes and edges in PyQT?

查看:223
本文介绍了我如何在PyQT中绘制节点和边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PyQT中,我如何在给定点绘制小节点并将它们与边连接起来?我发现的所有PyQT教程都是绘制一个按钮!绘制一个复选框!。

提前非常感谢

找到一个很好的解释是件很痛苦的事情(截至2014年底),而且由于这个问题要求我精确地寻找什么,我会发布我在这篇文章中找到的内容(从C ++到Python) / b>

代码如下,这里是基本原理:


  1. QGrahpicsItem QPainterPath QPainterPath.Element 是你正在寻找的课程。具体来说, QPainterPath 实现了您在应用程序中所需的矢量功能类型,例如CorelDraw,Adobe Illustrator或Inkscape。
  2. 以下示例受益于预先存在的 QGraphicsEllipseItem (用于渲染节点)和 QGraphicsPathItem (用于渲染路径本身),它继承自 QGraphicsItem

  3. 路径构造函数迭代 QPainterPath 元素,创建 Node 项为每一个;反过来,它们中的每一个都将更新发送到父路径对象,相应地更新其路径属性。

  4. 研究C ++ Qt4 Docs比在其他地方发现的结构更少的PyQt文档要容易得多。一旦习惯了C ++和Python之间的精神转换,这些文档本身就是了解如何使用每个类的强大方法。






 #!/ 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:

  1. QGrahpicsItem, QPainterPath and QPainterPath.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.
  2. The example below benefits from the pre-existing QGraphicsEllipseItem (for rendering nodes) and QGraphicsPathItem (for rendering the path itself), which inherit from QGraphicsItem.
  3. The Path constructor iterates over the QPainterPath elements, creating Node items for each one; Each of them, in turn, send updates to the parent Path object, which updates its path property accordingly.
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆