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. QTableWidget Drag and Drop works only once

QTableWidget Drag and Drop works only once

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 650 Views
  • 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.
  • UnitScanU Offline
    UnitScanU Offline
    UnitScan
    wrote on last edited by
    #1

    I made a QTableWidget subclass so can accept drag'n drop from external file. The only problem is accept the drag n drop only the first time: after this, it seems not works anymore.

    This is the subclass:

    Header

    #include <QWidget>
    #include <QTableWidget>
    #include <QDropEvent>
    #include <QDragMoveEvent>
    #include <QDropEvent>
    #include <QMimeData>
    #include <QDebug>
    
    #ifndef DTABLEWIDGET_H
    #define DTABLEWIDGET_H
    
    class DTableWidget : public QTableWidget
    {
        Q_OBJECT
    protected:
        void dragMoveEvent(QDragEnterEvent *e);
        void dropEvent(QDropEvent *event);
    public:
        explicit DTableWidget(QWidget *parent = 0);
    
        ~DTableWidget();
    
    public slots:
    
    private:
    
    private slots:
    
    };
    
    #endif // DTABLEWIDGET_H
    
    

    Source

    #include <dtablewidget.h>
    #include <QDrag>
    #include <QDragEnterEvent>
    #include <QMouseEvent>
    #include <QApplication>
    DTableWidget::DTableWidget(QWidget *parent) :
        QTableWidget(parent)
    {
        setAcceptDrops(true);
    }
    
    DTableWidget::~DTableWidget()
    {
    }
    
    void DTableWidget::dragMoveEvent(QDragEnterEvent *e)
    {
       e->acceptProposedAction();
    
    }
    
    void DTableWidget::dragEnterEvent(QDragEnterEvent *e)
    {
       e->acceptProposedAction();
    }
    
    void DTableWidget::dropEvent(QDropEvent *e)
    {
        QList<QUrl> urls = e->mimeData()->urls();
            foreach(QUrl url, urls)
            {
                qDebug()<<url.toString();
            }
    }
    

    Any solution?

    raven-worxR 1 Reply Last reply
    0
    • UnitScanU UnitScan

      I made a QTableWidget subclass so can accept drag'n drop from external file. The only problem is accept the drag n drop only the first time: after this, it seems not works anymore.

      This is the subclass:

      Header

      #include <QWidget>
      #include <QTableWidget>
      #include <QDropEvent>
      #include <QDragMoveEvent>
      #include <QDropEvent>
      #include <QMimeData>
      #include <QDebug>
      
      #ifndef DTABLEWIDGET_H
      #define DTABLEWIDGET_H
      
      class DTableWidget : public QTableWidget
      {
          Q_OBJECT
      protected:
          void dragMoveEvent(QDragEnterEvent *e);
          void dropEvent(QDropEvent *event);
      public:
          explicit DTableWidget(QWidget *parent = 0);
      
          ~DTableWidget();
      
      public slots:
      
      private:
      
      private slots:
      
      };
      
      #endif // DTABLEWIDGET_H
      
      

      Source

      #include <dtablewidget.h>
      #include <QDrag>
      #include <QDragEnterEvent>
      #include <QMouseEvent>
      #include <QApplication>
      DTableWidget::DTableWidget(QWidget *parent) :
          QTableWidget(parent)
      {
          setAcceptDrops(true);
      }
      
      DTableWidget::~DTableWidget()
      {
      }
      
      void DTableWidget::dragMoveEvent(QDragEnterEvent *e)
      {
         e->acceptProposedAction();
      
      }
      
      void DTableWidget::dragEnterEvent(QDragEnterEvent *e)
      {
         e->acceptProposedAction();
      }
      
      void DTableWidget::dropEvent(QDropEvent *e)
      {
          QList<QUrl> urls = e->mimeData()->urls();
              foreach(QUrl url, urls)
              {
                  qDebug()<<url.toString();
              }
      }
      

      Any solution?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @UnitScan

      void DTableWidget::dragMoveEvent(QDragEnterEvent *e)
      {
      ...
      }
      

      this method has the wrong signature. Thus it should never get called and thus the drag not accepted.

      In case you are using a C++11 compatible compiler (and Qt5) you should add Q_DECL_OVERRIDE macro at the end of all overrriding methods to avoid such mistakes.

      virtual void DTableWidget::dragMoveEvent(QDragMoveEvent *e) Q_DECL_OVERRIDE;
      

      In case there is something wrong, and you are not overriding anything the compiler will generate an error.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      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