Important: Please read the Qt Code of Conduct -

Issues trying to disable dragging of the buttons

  • Hello everybody,

    I made a Button class that inherits QGraphicsRectItem in order to have the button to be draggable accross the view. A button object is the added to a scene and it allows me to drag the buttons just like I want.

    My problem is that I want to be able to disable the dragging from a menu action item.

    I tried to make a method on the button class that will simply toggle the flag : self.setFlag(QGraphicsItem.ItemIsMovable, False)

    However when I use the method when I stored the objects in a list it results with this error.

    AttributeError: 'NoneType' object has no attribute 'disable_dragbuttons'

    Could anybody point me in the right direction?

    Something to consider I am using Python with PySide2.

  • Lifetime Qt Champion


    Can you show how you create these buttons and how to you put them it that list ?

  • Yes I will, I am using python though. Here is the code.

    class Button(QGraphicsRectItem):
        def __init__(self, text, *args): 
             QGraphicsRectItem.__init__(self, *args)
             #allows buttons to be dragged
             self.setFlag(QGraphicsItem.ItemIsMovable, True)
          def disable_button_drag(self):
                self.setFlag(QGraphicsItem.ItemIsMovable, True)  #disable dragging
    #Adding 10 buttons to list and at scene 
    def load_buttons(self):
          self.buttons = [self.scene.addItem(Button("name",coords)) for i in range(10)]

    This results in 10 buttons drawn at the QGraphicsView and are draggable. Issue is that I cant modify the buttons afterward because of the Nonetype error.

  • Lifetime Qt Champion

    What do you get if you print(self.buttons) after calling load_buttons ?

  • @SGaist

    Hi I get a NoneType.

    I found the issue turns out that I am calling a method inside that array. <doh!>

    I did this now and it works.

    def load_buttons(self):
       self.buttons = [Button(name,coords) for i in range(10)]
       for button in buttons:

    Now instead of printing None it now prints an object address.

    After doing this the buttons now work as intended.

    Thank you guys, you are really good.

Log in to reply