Dropping a file from outside an application into Q

2020-05-06 16:46发布

问题:

I have a QTableView and I'd like to be able to drop an image file from outside the application onto a row and upon this event it needs to identify which row it was dropped on, do things with the file (copy to a directory, resize, add to db, ...) and finally update a column in the table that this item/row now has an image file associated to it.

I've figured out that I can inherit from e.g. QTableView, re-implement dropEvent(QDropEvent*), I could then call QDropEvent::pos() inside the member function to get the QPoint for where the drop happened and then call QTableView::indexAt(const QPoint&) to get the QModelIndex for the row/column, but then I don't know how to inform the model with 1) the QModelIndex and 2) the QMimeData so that the model could handle those operations with the image and also update itself with the new data and provide that data to the view.

回答1:

bool QAbstractItemModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ) [virtual] the exact function to inform the model

  1. the QModelIndex and
  2. the QMideData

To update the model - Please read this: http://doc.qt.nokia.com/4.7-snapshot/qabstractitemmodel.html#dropMimeData and this one may help a lot (if you are not familiar yet) http://www.informit.com/articles/article.aspx?p=1405546