class MyTableView: public QTableView {
MyTableView(...) {
setDragEnabled(true);
setAcceptDrops(true);
setDragDropMode(DragDrop);
setDropIndicatorShown(true);
protected:
void dragEnterEvent(QDragEnterEvent *event) override {
// This function is called
const QMimeData* mimeData = event->mimeData();
QStringList test = mimeData->formats();
if (event->source() == nullptr) {
if (mimeData->hasUrls()) {
event->acceptProposedAction();
void dropEvent(QDropEvent *event) override {
// This function is not called.
class MyTableViewModel : public QAbstractTableModel {
Qt::ItemFlags flags(const QModelIndex& index) const {
if (!index.isValid())
return Qt::NoItemFlags;
Qt::ItemFlags flag = QAbstractItemModel::flags(index);
flag |= Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
return flag;
}
// Enable drop/drag in the constructor of MyTableView
setDragEnabled(true);
setAcceptDrops(true);
setDragDropMode(DragDrop);
setDropIndicatorShown(true);
// viewport()->setAcceptDrops(true); // it's not required although the doc mentioned it
// MyTableViewModel
Qt::ItemFlags MyTableViewModel::flags(const QModelIndex &index) const {
Qt::ItemFlags flag = QAbstractItemModel::flags(index);
if (index.isValid()) {
return flag | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
} else {
return flag | Qt::ItemIsDropEnabled;