python - PySide Node Graph connect items -


the purpose of code node graph kind of ui. double click creates rectangles , right click connects them line. if move on of rectangle corresponding end of line moves it. problem line moves first 2 rectangles. if create pair of rectangles , connect them line stops moving. have started learn pyside/pyqt maybe don't understand code.

here code: https://github.com/cyberirex/irex/blob/master/nodegraph

the code generates error following one:

self.selecteditems()[0].linktoitem = item self.selecteditems()[1].linktoitem = item 

let's 1 node connected link, when link added, previous link removed, why disconnected.

the logic continue generating similar errors solution propose new solution logic, base on following example elastic nodes example. can take basis understand logic of solution, have made variants meet requirements.

in first part have replaced base classes qgraphicsrectitem , qgraphicslineitem reduce implementation. logic based on giving each edge 2 node connect when updated take reference nodes. in addition each node stores list of edges executes update every time node moved. following code implements above:

class sceneclass(qgraphicsscene):     grid = 30      def __init__(self, parent=none):         qgraphicsscene.__init__(self, qrectf(-1000, -1000, 2000, 2000), parent)      def drawbackground(self, painter, rect):         painter.fillrect(rect, qcolor(30, 30, 30))         left = int(rect.left()) - int((rect.left()) % self.grid)         top = int(rect.top()) - int((rect.top()) % self.grid)         right = int(rect.right())         bottom = int(rect.bottom())         lines = []         x in range(left, right, self.grid):             lines.append(qline(x, top, x, bottom))         y in range(top, bottom, self.grid):             lines.append(qline(left, y, right, y))         painter.setpen(qpen(qcolor(50, 50, 50)))         painter.drawlines(lines)      def mousedoubleclickevent(self, event):         node = node()         self.additem(node)         node.setpos(event.scenepos())         qgraphicsscene.mousemoveevent(self, event)      def mousepressevent(self, event):         if event.button() == qt.rightbutton:             if len(self.selecteditems()) == 2:                 edge = edge(self.selecteditems()[0], self.selecteditems()[1])                 self.additem(edge)         qgraphicsscene.mousepressevent(self, event)   class node(qgraphicsrectitem):     def __init__(self, rect=qrectf(-75, -15, 150, 30), parent=none):         qgraphicsrectitem.__init__(self, rect, parent)         self.edges = []         self.setzvalue(1)         self.setbrush(qt.darkgray)         self.setflags(qgraphicsitem.itemismovable |                       qgraphicsitem.itemisselectable |                       qgraphicsitem.itemsendsgeometrychanges)      def addedge(self, edge):         self.edges.append(edge)      def itemchange(self, change, value):         if change == qgraphicsitem.itemselectedchange:             self.setbrush(qt.green if value else qt.darkgray)          if change == qgraphicsitem.itempositionhaschanged:             edge in self.edges:                 edge.adjust()          return qgraphicsitem.itemchange(self, change, value)   class edge(qgraphicslineitem):     def __init__(self, source, dest, parent=none):         qgraphicslineitem.__init__(self, parent)         self.source = source         self.dest = dest         self.source.addedge(self)         self.dest.addedge(self)         self.setpen(qpen(qt.red, 1.75))         self.adjust()      def adjust(self):         self.preparegeometrychange()         self.setline(qlinef(self.dest.pos(), self.source.pos())) 

obtaining shown in following image:

enter image description here

the complete example can found in following link


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -