QTableWidget Drag and Drop works only once



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


  • Moderators

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


Log in to reply
 

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