问题
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