[solved]How could I drop external items into QTableView?



  • Trying to drop some external files into the tableview with no avail. Following are the examples after simplify

    At first, I override dragEnterEvent and dropEvent of the QTableView

    dropAbleTableView.hpp

    #include <QTableView>
    
    class dropAbleView : public QTableView
    {
    public:
        dropAbleView(QWidget *parent = nullptr);
        ~dropAbleView();
    
    protected:
        void dragEnterEvent(QDragEnterEvent *event);
        void dropEvent(QDropEvent *event);
    };
    

    dropAbleTableView.cpp

    #include <QDebug>
    #include <QDragEnterEvent>
    #include <QDropEvent>
    
    #include "dropAbleView.hpp"
    
    dropAbleView::dropAbleView(QWidget *parent) :
        QTableView(parent)
    {
    
    }
    
    dropAbleView::~dropAbleView()
    {
    
    }
    
    void dropAbleView::dragEnterEvent(QDragEnterEvent *event)
    {
        qDebug()<<__FUNCTION__;
        event->acceptProposedAction();
    }
    
    void dropAbleView::dropEvent(QDropEvent *event)
    {
        qDebug()<<__FUNCTION__;
        event->acceptProposedAction();
    }
    

    According to the document, this is not enough, I have to override model too, to simplify the example, I use QStandrdItemModel

    #include <QStandardItemModel>
    
    class standardModel : public QStandardItemModel
    {
    public:
        standardModel(QObject *parent = nullptr);
        ~standardModel();
    
        bool canDropMimeData(const QMimeData *data,
        Qt::DropAction action, int row, int column, const QModelIndex &parent) const override;
    
        bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
                                const QModelIndex &parent);
    
        Qt::ItemFlags flags(const QModelIndex &index) const override;
    
        Qt::DropActions supportedDropActions() const override;
    };
    

    standardModel.cpp

    #include <QDebug>
    #include "standardModel.hpp"
    
    standardModel::standardModel(QObject *parent) :
        QStandardItemModel(parent)
    {
         setHorizontalHeaderItem(0, new QStandardItem(tr("String")));
    }
    
    standardModel::~standardModel()
    {
    
    }
    
    bool standardModel::canDropMimeData(const QMimeData*, Qt::DropAction, int, int, const QModelIndex&) const
    {
         qDebug()<<__FUNCTION__;
         return true;
    }
    
    bool standardModel::dropMimeData(const QMimeData*, Qt::DropAction, int, int, const QModelIndex&)
    {
        qDebug()<<__FUNCTION__;
        return true;
    }
    

    main.cpp

    #include "dropAbleView.hpp"
    #include "standardModel.hpp"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        dropAbleView view;
        view.setAcceptDrops(true);
        view.setDropIndicatorShown(true);
    
        standardModel model;
        model.appendRow(new QStandardItem("abcde"));
        view.setModel(&model);
        view.show();
    
        return a.exec();
    }
    

    However, no matter how hard I try, I cannot drop the item, the dropEvent of the dropAbleView never called, but the dragEnterEvent always called when I drag something into the view.

    The location (github)of the example(can compile and run )

    What do I lack to make it accept the items(text, files, folder etc) I drop?Thank you



  • Hi,
    Fixed it for you. You need to encapsulate your View into a MAinWindow or some other form of Widget that will control a window border/view etc I suppose.
    Then the drag/drop events are called in my debugger.
    Check out your main.cpp now.

    #include "dropAbleView.hpp"
    #include "standardModel.hpp"
    #include <QApplication>
    #include <QMainWindow>
    
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    
    QMainWindow MyWindow;
    MyWindow.setAcceptDrops(true);
    
    dropAbleView view;
    view.setAcceptDrops(true);
    view.setDropIndicatorShown(true);
    
    standardModel model;
    model.appendRow(new QStandardItem("abcde"));
    view.setModel(&model);
    view.show();
    
    MyWindow.setCentralWidget(&view);
    
    MyWindow.show();
    return a.exec();
    }
    

    <pre/></code>



  • @Jeroentjehome
    Thanks for your helps. I replace the main.cpp with your codes and run again, but the dropEvent still cannot called(drag event still work)

    I update the main.cpp on github,



  • Find out the answer, to get the dropEvent of dropAbleView be called, I need to override the model function
    "QStringList mimeTypes() const" and "bool dropMimeData(const QMimeData*, Qt::DropAction, int, int, const QModelIndex&)"

    you can check the working version from github


Log in to reply
 

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