Congratulations to our 2022 Qt Champions!

detect drop event in qt creator

  • Hi All,
    I have two qtable views. ui->table_view and ui->table_view2, I have set up to drag and drop from table_view to table_view2. This works OK but always drops to the bottom of table_view2. I've searched everywhere and tried everything to get it to drop over or in between, but it won't.
    So what I thought what I'd do is try to detect when I do a drop event over the table_view2, then find position. However in creator I can't find a slot on the table view that appears to do this.
    Can anyone advise how I find out if an item is dropped over ui->table_view2?


  • Lifetime Qt Champion

    the drag and drop are functions u can override for a custom class
    dragEnterEvent -> something being dragged into views area
    dragMoveEvent -> something being dragged inviews area
    dropEvent -> something being dropped into views area

    so dropEvent would be called when u drop the item on say view2

    with view, the dropped data is handles by the model
    so if u goal is to insert between 2 other items
    then have a look at

  • Hi,
    thank you for the answers, but I'm still slightly puzzled :s.

    Are these functions meant to "override" some low level function, if not how do I tell them to be called? Is something set in the dialog constructor? For example I added them to my table edit dialog, and added a simple qdebug to say I've entered the function, like say I would on a thread or slot, just a test to say "hello world" I'm called. However when I start up my code moving items on the table never calls these?

    void Tables_Edit_Dialog::dropMoveEvent(QDropEvent *event)
    bool Tables_Edit_Dialog::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
        qDebug()<<"here 2";


  • Lifetime Qt Champion

    @tony67 said:
    they override the default ones.
    They are called by Qt drag&drop system.

    It seems fine for your dialog, but did u remember to
    enable drag & drop for it ?
    else these functions are never called.
    setAcceptDrops(true); in ctor of dialog

    also, im not sure dropMimeData is valid. I have only used that for a model
    NOT a dialog. seems wrong.

    maybe see this

  • Thanks I'll give the video a watch. I've tried
    in my constructor. But no luck, though I think they must be set as I can drag and drop at the bottom of the list. I'm wondering if it's because I've used designer to set up the tableview drag and drops (I've tried every possible combination ) or if its becuase I'm using Linux. But fear it's just my misunderstanding.

  • Lifetime Qt Champion

    First of all.
    It dont matter if you setup in Designer
    or in code.
    As long as u set it after setup_UI

    Also I think you should really read
    and understand its not the view but the model that handles the
    placement of the dropped item.

    Will enable drops to the view. Im not sure what else u expect it to do.

  • If you want customized drag and drop behavior you have to use a custom data model. Subclass QAbstractItemModel and implement/override the behaviours you want to change. Qt documentation is all what you need.

Log in to reply