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. [solved]How could I drop external items into QTableView?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.5k 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.
  • S Offline
    S Offline
    stereomatchingkiss
    wrote on last edited by stereomatchingkiss
    #1

    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

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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>

      Greetz, Jeroen

      S 1 Reply Last reply
      0
      • JeroentjehomeJ Jeroentjehome

        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>

        S Offline
        S Offline
        stereomatchingkiss
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stereomatchingkiss
          wrote on last edited by stereomatchingkiss
          #4

          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

          1 Reply Last reply
          0

          • Login

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