Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created August 13, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/168aced67ffc2df0afb85e1e58b0eaff to your computer and use it in GitHub Desktop.
Save eyllanesc/168aced67ffc2df0afb85e1e58b0eaff to your computer and use it in GitHub Desktop.
You have to enable the ItemIsSelectable flag: self.setFlag(QGraphicsItem.ItemIsSelectable, True)
from PySide.QtCore import *
from PySide.QtGui import *
rad = 5
class WindowClass(QMainWindow):
def __init__(self):
super(WindowClass, self).__init__()
self.view = ViewClass()
self.setCentralWidget(self.view)
class ViewClass(QGraphicsView):
def __init__(self):
super(ViewClass, self).__init__()
self.setDragMode(QGraphicsView.RubberBandDrag)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.s = SceneClass()
self.setScene(self.s)
self.setRenderHint(QPainter.Antialiasing)
class SceneClass(QGraphicsScene):
def __init__(self):
super(SceneClass, self).__init__()
self.setSceneRect(-1000, -1000, 2000, 2000)
self.grid = 30
def drawBackground(self, painter, rect):
if False:
painter = QPainter()
rect = QRect()
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 = []
for x in range(left, right, self.grid):
lines.append(QLine(x, top, x, bottom))
for 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 mouseReleaseEvent(self, event):
print self.selectedItems()
print len(self.selectedItems())
print "release"
super(SceneClass, self).mouseReleaseEvent(event)
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
self._start = event.scenePos()
path = QPainterPath()
path.moveTo(self._start.x(), self._start.y())
path.lineTo(self._start.x() + 1.0, self._start.y() + 1.0)
self.addItem(Path(path, self))
if event.button() == Qt.LeftButton:
print self.selectedItems()
print len(self.selectedItems())
print "press"
# I need here to get selected obejcts name and position
# QGraphicsEllipseItem and path element
super(SceneClass, self).mousePressEvent(event)
class Node(QGraphicsEllipseItem):
def __init__(self, path, index):
super(Node, self).__init__(-rad, -rad, 2*rad, 2*rad)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
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)
return QGraphicsEllipseItem.itemChange(self, change, value)
class Path(QGraphicsPathItem):
def __init__(self, path, scene):
super(Path, self).__init__(path)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
for i in range(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 = self.path()
path.setElementPositionAt(index, pos.x(), pos.y())
self.setPath(path)
if __name__ == '__main__':
app = QApplication([])
wd = WindowClass()
wd.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment