Hello everyone! This is my first post here. Using search I found that here, one can post PyQt questions but I'm using PySide. I don't know If I'm breaking the rules of this forum. If so I ask moderators to remove this post.
I'm very new in programming and never used C++ but writing this code my guide was QT documentation because it is more descriptive than PyQt.
What I'm looking for is to create node graph kind of UI. I have realized nodes and connections. It works fine with two items connected in the scene but when I create two more nodes it does not work properly. I mean if you create two nodes with double click, then select them and do right click you'll see a path between this two nodes. If you move one of the node the pathElements will follow the nodes. Now add two more nodes and connect them. You'll see that newly created path does not transform.
Double click = creates nodes. Selecting 2 nodes and right click creates path item between 2 selected items.
Here is the code:

Qt Code:
  1. from PySide.QtCore import *
  2. from PySide.QtGui import *
  3.  
  4. rad = 5
  5.  
  6. class WindowClass(QMainWindow):
  7. def __init__(self):
  8. super(WindowClass, self).__init__()
  9. self.view = ViewClass()
  10. self.setCentralWidget(self.view)
  11.  
  12.  
  13. class ViewClass(QGraphicsView):
  14. def __init__(self):
  15. super(ViewClass, self).__init__()
  16.  
  17. self.setDragMode(QGraphicsView.RubberBandDrag)
  18. self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  19. self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  20.  
  21. self.s = SceneClass()
  22. self.setScene(self.s)
  23. self.setRenderHint(QPainter.Antialiasing)
  24.  
  25.  
  26. class SceneClass(QGraphicsScene):
  27. def __init__(self, id=None):
  28. super(SceneClass, self).__init__()
  29. self.setSceneRect(-1000, -1000, 2000, 2000)
  30. self.grid = 30
  31. self.it = None
  32. self.node = None
  33.  
  34. def drawBackground(self, painter, rect):
  35. if False:
  36. painter = QPainter()
  37. rect = QRect()
  38.  
  39. painter.fillRect(rect, QColor(30, 30, 30))
  40. left = int(rect.left()) - int((rect.left()) % self.grid)
  41. top = int(rect.top()) - int((rect.top()) % self.grid)
  42. right = int(rect.right())
  43. bottom = int(rect.bottom())
  44. lines = []
  45. for x in range(left, right, self.grid):
  46. lines.append(QLine(x, top, x, bottom))
  47. for y in range(top, bottom, self.grid):
  48. lines.append(QLine(left, y, right, y))
  49. painter.setPen(QPen(QColor(50, 50, 50)))
  50. painter.drawLines(lines)
  51.  
  52. def addNode(self, pos=False):
  53. if not pos:
  54. pos = QPoint(0, 0)
  55. item = ItemClass(self.grid, len(self.items()))
  56. self.addItem(item)
  57. item.setPos(pos)
  58.  
  59.  
  60. def mouseDoubleClickEvent(self, event):
  61. self.addNode(event.scenePos())
  62. super(SceneClass, self).mouseDoubleClickEvent(event)
  63.  
  64.  
  65. def mousePressEvent(self, event):
  66. if event.button() == Qt.RightButton:
  67. self._start = event.scenePos()
  68. #####
  69. path = QPainterPath()
  70. path.moveTo(self.selectedItems()[0].pos().x(), self.selectedItems()[0].pos().y())
  71. path.lineTo(self.selectedItems()[1].pos().x(), self.selectedItems()[1].pos().y())
  72. item = Path(path, self)
  73.  
  74. self.selectedItems()[0].linkToItem = item
  75. self.selectedItems()[1].linkToItem = item
  76.  
  77. self.addItem(item)
  78.  
  79. super(SceneClass, self).mousePressEvent(event)
  80.  
  81.  
  82. class Path(QGraphicsPathItem):
  83. def __init__(self, path, scene):
  84. super(Path, self).__init__(path)
  85. self.setPen(QPen(Qt.red, 1.75))
  86.  
  87. def updateElement(self, index, pos):
  88. path = self.path()
  89. path.setElementPositionAt(index, pos.x(), pos.y())
  90. self.setPath(path)
  91.  
  92.  
  93. class ItemClass(QGraphicsItem):
  94. def __init__(self, height, index ):
  95. super(ItemClass, self).__init__()
  96. self.x = 0
  97. self.y = 0
  98. self.w = 150
  99. self.h = height
  100. self.hover = None
  101. self.setAcceptHoverEvents(True)
  102. self.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable)
  103. self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
  104.  
  105. self.linkToItem = None
  106.  
  107. self.index = index
  108. self.setZValue(1)
  109.  
  110. def itemChange(self, change, value):
  111.  
  112. if change == QGraphicsItem.ItemPositionChange:
  113. if self.linkToItem:
  114. self.linkToItem.updateElement(self.index, value)
  115.  
  116. return QGraphicsItem.itemChange(self, change, value)
  117.  
  118. def boundingRect(self):
  119. return QRect(self.x, self.y, self.w, self.h)
  120.  
  121. def paint(self, painter, opt, w):
  122. rec = self.boundingRect()
  123. if False:
  124. painter = QPainter()
  125. if not self.hover:
  126. color = Qt.darkGray
  127. else:
  128. color = Qt.gray
  129. if self.isSelected():
  130. color = Qt.yellow
  131.  
  132.  
  133. painter.fillRect(rec.adjusted(3,3,-3,-3),color)
  134.  
  135. self.scene().update()
  136.  
  137.  
  138. if __name__ == '__main__':
  139. app = QApplication([])
  140. wd = WindowClass()
  141. wd.show()
  142. app.exec_()
To copy to clipboard, switch view to plain text mode