Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting row item is dropped at on a qtwidgettable
Forum Updated to NodeBB v4.3 + New Features

Getting row item is dropped at on a qtwidgettable

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tony67
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tony67
        wrote on last edited by
        #3

        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

        mrjjM 1 Reply Last reply
        1
        • T tony67

          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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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()-..
          }

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tony67
            wrote on last edited by
            #5

            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);

            mrjjM 1 Reply Last reply
            0
            • T tony67

              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);

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @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?

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tony67
                wrote on last edited by
                #7

                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?

                mrjjM 1 Reply Last reply
                0
                • T tony67

                  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?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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);

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    tony67
                    wrote on last edited by
                    #9

                    @mrjj said:

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

                    Thank's second method worked!

                    mrjjM 1 Reply Last reply
                    1
                    • T tony67

                      @mrjj said:

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

                      Thank's second method worked!

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved