本文介绍了QTableView外部拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将行拖出QTableView?

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

我知道如何在 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);

浏览 QAbstractItemView :: DragDropMode ,从直觉上讲,我希望以下尝试允许外部拖动垂直标题项目:

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.

给出我从 QSqlRelationalTableModel 派生了自己的模型,并将 Qt :: ItemIsDragEnabled 标志添加到标志中函数,用于所有垂直标头索引:

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;
}

这并没有改变任何内容,并且给出了调试打印信息,<$ c $甚至没有为垂直标题项调用c> flags

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

推荐答案

是否添加acceptDrops(true )到QTableView的帮助吗?

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

这篇关于QTableView外部拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 15:05