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!!!!";
        }
    
    

    Thanks


  • Qt Champions 2016

    Hi
    One way is to use the x,y location of the drop.
    Then ask the Table what item is at that place.
    https://forum.qt.io/topic/50495/solved-qtablewidget-drop-event-get-cell/4



  • 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


  • Qt Champions 2016

    @tony67
    Ok. maybe first tell me what function we are in ?
    The DropEvent has pos
    http://doc.qt.io/qt-5/qdropevent.html#pos

    You might need to cast the generic event to a DropEvent
    DropEvent *de=qobject_cast<DropEvent *>(event);
    if(de) {
    ...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);


  • Qt Champions 2016

    @tony67
    Hi
    Normally you would override the
    void YourTable::dropEvent(QDropEvent *event)
    virtual function.
    http://doc.qt.io/qt-5/dnd.html

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



  • yes using

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

    then

    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?


  • Qt Champions 2016

    @tony67
    U can also use eventfilter but if you look at
    http://doc.qt.io/qt-5/dnd.html
    you see that normally you be using
    QDragEnterEvent
    Event which is sent to a widget when a drag and drop action enters it
    QDragLeaveEvent
    Event that is sent to a widget when a drag and drop action leaves it
    QDragMoveEvent
    Event which is sent while a drag and drop action is in progress
    QDropEvent
    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>
    and
    QDropEvent *dropevent = qobject_cast< QDropEvent *>(event);

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



  • @mrjj said:

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

    Thank's second method worked!


  • Qt Champions 2016

    @tony67
    Super.
    Sorry my bad. Been using qobject_cast too much :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.