QTableView external drag and drop

断了今生、忘了曾经 提交于 2020-01-05 15:15:47

问题


Is there a way to drag rows out of a QTableView?

I know how to internally move columns within a QTableView by configuring some properties on the view:

table_view_->horizontalHeader()->setSectionsMovable(true);
table_view_->horizontalHeader()->setDragEnabled(true);
table_view_->horizontalHeader()->setDragDropMode(QAbstractItemView::InternalMove);

Going through the documentation on QAbstractItemView::DragDropMode, I intuitively expected my following attempt to allow external dragging of vertical header items:

table_view_->verticalHeader()->setSectionsMovable(true);
table_view_->verticalHeader()->setDragEnabled(true);
table_view_->verticalHeader()->setDragDropMode(QAbstractItemView::DragOnly);

Still, I am limited to only internal move.

Given this article I derived my own model from QSqlRelationalTableModel and added the Qt::ItemIsDragEnabled flag to the flags function, for all vertical header indexes:

Qt::ItemFlags MyRelationalTableModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags defaultFlags = QSqlRelationalTableModel::flags(index);

    if(index.column() == -1 && index.row() > -1) {
        qDebug() << "drag enabled.";
        return Qt::ItemIsDragEnabled | defaultFlags;
    } else {
        qDebug() << "not drag enabled.";
    }

    return defaultFlags;
}

This didn't change anything and given the debug prints, flags wasn't even called for vertical header items.


回答1:


Does adding acceptDrops(true) to the QTableView help at all?



来源:https://stackoverflow.com/questions/32342897/qtableview-external-drag-and-drop

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