Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

PyQt4.x Python 2.7 + QGraphicview with custom widget, move position with mouse



  • Hey

    Hope this is the right place.

    I have added a QGroupBox to QGraphicview via:

    self.Scene = QGraphicsScene()
    ab = CustomGroupBox() # Subclassed
    self.Scene.addWidget(ab)
    

    I then subclassed the QGroupBox with hoping that I can override

    mouseMoveEvent()
    

    With something like this :

    def mouseMoveEvent(self, event):
        super(CustomGroupBox, self).mouseMoveEvent(event)
        print event
        p = QtGui.QCursor.pos()
        pos_wid =  self.mapToGlobal(self.pos())
        self.move(p.x(),p.y())
    

    So that I can move the widget around in the scene. However every time I click and start dragging the widget jumps by xx pixels right/left anywhere depending where the window is. I know is an issue of positioning I just don't know how to solve it :- (.

    Any help would be amazing!
    Regards
    Dariusz


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    IIRC, that's already handled by the Graphics View Framework. Take a look at the GraphicsItemFlag and setFlag.

    Hope it helps



  • Hey

    Mmmm I'm lost... where do you want me to set the flag up ?

    I tried setting it up on my custom widget but that does nothing as far as I can tell.. When I disabled my mouseMoveEvent and just let ur flags I could not move icons at all...

    Here is video of the issues : https://vid.me/hwaa
    Here is most of my custom widget:

    class CustomGroupBox(QGroupBox):
        def __init__(self, parent=None):
            super(CustomGroupBox, self).__init__(parent)
    def mouseMoveEvent(self, event):
            super(CustomGroupBox, self).mouseMoveEvent(event)
            p = QtGui.QCursor.pos()
            self.move(p.x(),p.y())
    
    

    I think what I need is to somehow remap my mouse position to graphic view position and then on top of that offset widget position to mouse... but no idea how :- (


  • Lifetime Qt Champion

    On the item returned by self.Scene.addWidget(ab)



  • Hey

    Humh I must be missing something... when I tried :

            ab = QGroupBox()
            ab.setFixedWidth(200)
            xx = QGridLayout()
            wa = QPushButton("wow")
            xx.addWidget(wa)
            wa.clicked.connect(self.printWiw)
            ab.setLayout(xx)
            ab.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
            self.Scene.addWidget(ab)
    

    I got :

    ab.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
    AttributeError: 'QGroupBox' object has no attribute 'setFlag'
    

    When I tried to add it after scene :

    for items in self.Scene.items():
    items.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
    

    The item groupbox is still unmovable.

    Edit:
    Also tried:

    for items in self.Scene.items():
                items.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
                items.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
    

  • Lifetime Qt Champion

    Hi,

    This stackoverflow answer might give you some clues.


Log in to reply