Important: Please read the Qt Code of Conduct -

Getting row item is dropped at on a qtwidgettable

  • Hi All,
    I have two tables, I can copy items from the first to the second (a qtwidgettable) by dragging and dropping. This is working well. I've set up an event filter to detect when an item is dropped. However I'd like to know what row the item is dropped on and can't work this out. Can anyone explain how I can detect the row location of the drop please? My code below show's where I'd like to get the drop location.

        if( loc == ui->tableWidget->viewport() && event->type() == QEvent::Drop )
            qDebug() << "dropped in the table but where!!!!";


  • Lifetime Qt Champion

    One way is to use the x,y location of the drop.
    Then ask the Table what item is at that place.

  • Thanks for the reply, but I can't follow the answer :s. For example he get's position with

    int position = event->pos();

    but QEvent doesn't have anything called pos() ? I know it has type(), but where are they getting pos from?

    Sorry I'm having real trouble even goggling this, maybe I'm using wrong terms due to my limited knowledge of QT

  • Lifetime Qt Champion

    Ok. maybe first tell me what function we are in ?
    The DropEvent has pos

    You might need to cast the generic event to a DropEvent
    DropEvent *de=qobject_cast<DropEvent *>(event);
    if(de) {>pos()-..

  • Thanks for helping. I've tried to use DropEvent, it says the class doesn't exist? Should I be using QDropEvent?
    If I do that I get another error

    QDropEvent *dropevent = qobject_cast< QDropEvent *>(event);

    edit_dialog.cpp:107: error: invalid use of non-static member function
    QDropEvent *dropevent = qobject_cast< QDropEvent *>(event);

  • Lifetime Qt Champion

    Normally you would override the
    void YourTable::dropEvent(QDropEvent *event)
    virtual function.

    Not sure how u do it now. Is it an event filter?

  • yes using

    ui->tableWidget->viewport()->installEventFilter( this );


    bool Edit_Dialog::eventFilter( QObject* object, QEvent* event )
        if( object == ui->tableWidget->viewport() && event->type() == QEvent::Drop )

    I thought this was the correct way to do things from reading?

  • Lifetime Qt Champion

    U can also use eventfilter but if you look at
    you see that normally you be using
    Event which is sent to a widget when a drag and drop action enters it
    Event that is sent to a widget when a drag and drop action leaves it
    Event which is sent while a drag and drop action is in progress
    Event which is sent when a drag and drop action is completed

    which are all virtual functions where the Event is of the right type.

    However, lets focus on eventfilter

    if you
    #include <QDropEvent>
    QDropEvent *dropevent = qobject_cast< QDropEvent *>(event);

    It still complains about the cast?
    I might remember wrong.
    QDropEvent *dropevent = dynamic_cast< QDropEvent *>(event);

  • @mrjj said:

    QDropEvent *dropevent = dynamic_cast< QDropEvent *>(event);

    Thank's second method worked!

  • Lifetime Qt Champion

    Sorry my bad. Been using qobject_cast too much :)

Log in to reply