Important: Please read the Qt Code of Conduct -

Generating mousePress event for drag event

  • Hello folks,

    I have drag-n-drop working in my custom QWidget subclass. Everything is working fine so far. The user can drag-n-drop items around similar to the fridge magnets example.
    Now, what I added is a Duplicate option to the custom context menu of the item. That menu entry (action) just calls a slot in which I generate and execute the QDrag exactly the same as I would do in my mouseMoveEvent() function. That works too, the drag gets executed properly. The problem I have is that the slot is being called when the user releases the mouse button after he pressed it down on the Duplicate menu entry in the context menu. The release of the mouse button causes the slot to be executed. The drag is created but the item is dropped immediately because the mouse button is not held down. Therefore, the user doesn't get a change to position the item with his mouse before manually dropping it. Instead, the duplicated item is dropped exactly where the Duplicate context menu entry has been clicked.

    Is there any way around this? Ideally I'd like to archive the behavior that the user clicks the Duplicate context menu entry, then moves the mouse around to position the item and then clicks to drop it.

    I tried several things including executing qApp->processEvents() before the slot is called. Without luck.

  • Lifetime Qt Champion


    Maybe not a direct answer however I'm rather used that when I ask for a duplicate on a graphic editing program, the duplicated object gets placed on the screen slightly displaced regarding the original object. Then you can grab it at will.

  • Thank you for your suggestion. I ended up implementing the behavior that you described to have something that works as expected.

  • Lifetime Qt Champion


    I can only second that. I use many drawing programs and those who has clone, all offset
    the clone and then u must then drag it.

    Having it stick to the mouse after you release it, would be confusing. :)

  • Thank you for your comment! Apparently the proper fix to this problem is me stopping to have weird-behavior expectations of my software :p

  • Lifetime Qt Champion


    Well actually its not super weird. If you active
    accessibility settings in windows, (on some pads)
    you can get this for drag & drop to help
    people where is hard to hold down mouse. :)