Reputation: 28
I am writing an application that displays a list of files in a QTableView. I have subclassed QTableView to my own view (fileBrowserTableView). The aim of this table is to display a list of files and folders. I would like the user to be able to select a cell (or selection of cells) and drag this to an external application that accepts MIMEs of that type (i.e. Windows Explorer or Outlook). From my research it seems that I need to re-implement mousePressEvent and mouseMoveEvent. Within these functions I can then create a QDrag object and set the QMimeData to be of type QList, which relates to the selected files.
Does anyone have any suggestions or code examples of doing this? All the existing samples I have found seem to focus on dropping rows between widgets or into the same widgets. I am not interested in any drop functionality onto this widget at the moment. The other thing I am keen to preserve is all the existing selection behaviour of this widget - only when the drag begins do I want this behaviour to take place. At the moment my initial attempts to re-implement mousePressEvent have meant I have lost all selection behaviour in my table. I have set dragEnabled on my table, so I can drag cells around under my mouse, although at the moment I can obviously not drop them anywhere.
Thanks in advance.
Upvotes: 0
Views: 430
Reputation: 7146
Take a look at QAbstractItemModel::mimeData
. If you reimplement this in your model, you can easily handle drags. As soon as you start the drag, this function will be called with the selected indexes, without having to reimplement anything of it in the view.
All thats left to do is to use QMimeData
to add the file-list to it.
Example:
QMimeData *MyModel::mimeData(const QModelIndexList &indexes) const {
if(indexes.isEmpty())
return Q_NULLPTR;
QMimeData *data = new QMimeData();
QList<QUrl> files;
foreach(QModelIndex index, indexes)
files += QUrl::fromLocalFile(this->getFileName(index));
data->setUrls(files);
return data;
}
Upvotes: 1