简体   繁体   English

QTreeWidget使用拖放在两个项目之间插入

[英]QTreeWidget insert between two items using drag and drop

I have a QTreeWidget which I have setup like so... 我有一个QTreeWidget,它的设置如下:

ModelTreeWidget::ModelTreeWidget(QWidget *parent):QTreeWidget(parent), mpModelStruct(nullptr), mpModeldragging(nullptr), mpBufferModel(nullptr), mpModelCurrent(nullptr)
{

    setEditTriggers(QAbstractItemView::SelectedClicked | QAbstractItemView::EditKeyPressed);
    setColumnCount(1);
    setSelectionMode(QAbstractItemView::SingleSelection);

    setDragEnabled(true);
    viewport()->setAcceptDrops(true);
    setDropIndicatorShown(true);
    setDragDropMode(QAbstractItemView::DragDrop);
    setDefaultDropAction(Qt::MoveAction);

}

I've also overwritten my drop event, but it's fairly innocuous: 我也覆盖了drop事件,但它是无害的:

void ModelTreeWidget::dropEvent(QDropEvent* event)
{
    QTreeWidget::dropEvent(event);
}

Supported action is MoveAction: 支持的动作是MoveAction:

Qt::DropActions ModelTreeWidget::supportedDropActions() const
{
    return Qt::MoveAction;
}

Each item has the flags: 每个项目都有标志:

Qt::ItemFlags ModelTreeWidget::getTreeItemFlags() const
{
    return (Qt::ItemIsDragEnabled | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}

Now when I populate my tree widget with items, I can drag one item over another, and it becomes added as a child to the item I dragged onto. 现在,当我在树小部件中填充项目时,我可以将一个项目拖到另一个项目上,然后它作为子项添加到我拖到的项目上。 However when I try to drag an item between two other items, I don't get an indicator (which I think should be a line) between the two rows. 但是,当我尝试在其他两个项目之间拖动一个项目时,在两行之间没有显示指示器(我认为应该是一条线)。 I just get the not allowed indicator. 我刚刚得到了不允许的指标。

My question is, how do I enable insertion of an item between two other items using drag and drop for QTreeWidget? 我的问题是,如何通过拖放使用QTreeWidget在其他两个项目之间插入项目?

Edit: I setup my tree root using the following code: 编辑:我使用以下代码设置我的树根:

        QTreeWidgetItem* rootId = new QTreeWidgetItem((QTreeWidget*)0, QStringList(currentModel->name));
        QVariant v = qVariantFromValue((void *) currentModel);
        rootId->setData(0, Qt::UserRole, v);
        rootId->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        insertTopLevelItem(0, rootId);

While each child of my root is setup as the following: 虽然我的根的每个孩子都按以下步骤进行设置:

            QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(tempModel->name));
            QVariant v = qVariantFromValue((void *) tempModel);
            item->setData(0, Qt::UserRole, v);
            item->setFlags(getTreeItemFlags());
            parent->addChild(item);

It seems that there are two problems here. 看来这里有两个问题。 One is that my supportedDropActions() only supported MoveAction, which seems to interfere with the drag n drop insert function, which add and delete data around. 一是我的supportedDropActions()仅支持MoveAction,这似乎干扰了拖放插入功能,该功能添加和删除周围的数据。 The symptoms of this were that each time I inserted data between two entries in a basic test QTreeWidget class, it would also erase the element before the object I was dragging. 这样做的症状是,每次我在基本测试QTreeWidget类的两个条目之间插入数据时,它也将擦除要拖动的对象之前的元素。 By adding Qt::CopyAction so that my supportedDropActions() looks like 通过添加Qt :: CopyAction,使我的supportedDropActions()看起来像

Qt::DropActions ModelTreeWidget::supportedDropActions() const
{
    return (Qt::MoveAction | Qt::CopyAction);
}

Fixed the prob of drag and drop insertions between entries deleting an entry each time it moved. 修复了条目之间拖放插入的问题,每次移动时都会删除条目。 However, this didn't fix my actual problem asked in this post. 但是,这不能解决我在这篇文章中提出的实际问题。

What fixed my actual problem was not manually setting my tree root's flags. 解决我的实际问题的方法不是手动设置树根标志。

        QTreeWidgetItem* rootId = new QTreeWidgetItem((QTreeWidget*)0, QStringList(currentModel->name));
        QVariant v = qVariantFromValue((void *) currentModel);
        rootId->setData(0, Qt::UserRole, v);
//Commented this out        rootId->setFlags(Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        insertTopLevelItem(0, rootId);

Doing so solved my problem. 这样做解决了我的问题。 I'm still not completely sure why that would interfere with the insertion operation but it did. 我仍然不太确定为什么这会干扰插入操作,但确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM