Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem and dragEvents
Qt 6.11 is out! See what's new in the release blog

QGraphicsItem and dragEvents

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GofferG Offline
    GofferG Offline
    Goffer
    wrote on last edited by
    #1

    Hey Community,

    I can't get my drag events to work on QGraphicsItem. Even with the setAcceptDrops(True), is there something else to do to enable them ? They work by default on regular widgets...
    Since QGraphicsItems are movable, I understand that it might be a little bit different but can't figure this out.

    here is a sample code :

    from PySide import QtGui, QtCore
    
    class GraphicItem(QtGui.QGraphicsItem):
    
        def __init__(self, parent=None):
            super(GraphicItem, self).__init__(parent)
    
            self.setAcceptDrops(True)
    
        def boundingRect(self):
            rect = QtCore.QRect(0,0,400,400)
            return rect
    
        def shape(self):
            path = QtGui.QPainterPath()
            path.addRect(self.boundingRect())
            return path
    
        def paint(self, painter, option, widget):
            painter.setBrush(QtGui.QColor(255,0,0,255))
            painter.drawRect(self.boundingRect())
    
        def dragEnterEvent(self, event):
            print 'drag enter'
            super(GraphicItem, self).dragEnterEvent(event)
    
        def dragMoveEvent(self, event):
            print 'drag move'
            super(GraphicItem, self).dragMoveEvent(event)
    
    
    s = QtGui.QGraphicsScene()
    v = QtGui.QGraphicsView(s)
    v.show()
    
    ti = GraphicItem(parent=None)
    s.addItem(ti)
    

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you want to move your items around ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • GofferG Offline
        GofferG Offline
        Goffer
        wrote on last edited by
        #3

        Nope, It is just a static item that it driven by a parent.
        For now I have to do it with mouse press/move/release events + a condition ...
        It works but i feel like the drag events are designed for that.

        Or, am I wrong ?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then what exactly do you want to achieve using drag and drop ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved