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

Stick a widget to the mouse while Drag N Drop?



  • Hey,

    I'm trying to make a widget Drag N Dropable. Works fine so far, all the events get fired properly. But I want a cool "animated" drag widget which sticks to the mouse as the user is dragging. I thought maybe the best way is to start a thread which repeatedly sets the widget to the cursor position as soon as the widget is clicked and dragged.
    I'd start it in the mouseMoveEvent(QMouseEvent *e) and would set the stop flag in the dropEvent(QDropEvent *e). So I havent looked at the threads yet and if you got any tips there I would appreciate it, but my more important problem is that I don't know how to set a widget absolutely to a position. It should be ripped out of it's layout and then be displayed above every other widget, as long as the drag goes.
    Is that the way to approach it? Or am I missing a thing here?

    Alternatively I thought about just setting the cursor to a corresponding Pixmap, but that would prevent any animations and seems a bit hacky to me.

    Thanks for any suggestions :D


  • Lifetime Qt Champion

    Hi,

    If you are creating your own QDrag object then you can use QDrag::setPixmap and put there the rendered version of your widget.



  • Okay, thank you, that kinda works.
    GridDrag.png
    But now I've got this ugly background and the hover effect is captured when I start dragging it. I probably need to store a not interacted pixmap in my object when it gets created so I haven't got the hover effect in my render. But do you have an idea how to get rid of the background? I've found a thread, you contributed to it, but I couldn't really understand what I have to do..
    Thanks for your help!

    Edit: So that worked for me. Got rid of the hover effect by simply rendering the widget in it's show event and storing the pixmap inside a member. Still got that background tho.



  • @m1212e said in Stick a widget to the mouse?:

    Still got that background tho

    Have you tried to set a translucent background before rendering your widget? I think, this is what the OP in your linked thread also did

    setAttribute(Qt::WA_TranslucentBackground);
    


  • Must have missed it. Thank you very much! Works fine now :D



  • @m1212e said in Stick a widget to the mouse?:

    but I couldn't really understand what I have to do

    The code sample posted by the OP shows a section from a version control system (like Git) change log file.
    The + (green background) lines indicate insertions.
    So, if you know what to look for, it takes 3 seconds, until you know what's going on there :-)
    In case you didnt know that :-)


Log in to reply