Dropping a file from outside an application into QTableView

☆樱花仙子☆ 提交于 2020-01-30 11:56:26

问题


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



来源:https://stackoverflow.com/questions/9672629/dropping-a-file-from-outside-an-application-into-qtableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!