Important: Please read the Qt Code of Conduct -

Drag and Drop into empty widget space

  • I would like to know whether or not it is possible to drag and drop into (what I want to refer to as) empty widget space.

    To be more precise:

    I have defined a MultiTrackPlayer class derived from QWidget. This Player dynamically adds a PlayerControls (play button/pause button/...) for each track added to it. At this point adding new PlayerControls is triggered by button press and then delivering a QUrl, which references the media file to be played, to the MultiTrackPlayer. I would like to replace the button click, by dragging Urls into the yet to be filled layout space of my MultiTrackMediaPlayer.

    I successfully implemented drag and drop on my custom QListView, overriding

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void dragEnterEvent(QDragEnterEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);

    and setting


    Using the same procedure on MultiTrackPlayer does not give me any ability to perform the above described behaviour. I am assuming that is because the empty layout space is not treated as a legal drop site!?

    How would I handle this correctly, can anyone point me into the right direction?

    Thanks! =)

  • Lifetime Qt Champion

    can u show the empty widget space?

  • @mrjj
    The empty space you see in the attached image is basically the layout which all PlayerControls are added to, held by a QGroupBox titled "Players:".
    That being said, do I have to configure my drag and drop functionality on the QGroupBox rather then the MultiTrackPlayer?

  • Okay I added a class derived from QGroupBox to host my drop events

    void dragEnterEvent(QDragEnterEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void dropEvent(QDropEvent *event);

    using this groupbox to hold the MultiTrackPlayer layout gives me the functionality I was looking for. Thanks @mrjj for the comment. Replying to it inspired me to try that out.


  • Lifetime Qt Champion

    Heh. good work.

    Just as a note:
    you can use event filters to implement drag and drop on other widgets if
    no other need to subclass them.

    But for your case, subclassing QGroupBox is super.