拖放两个QTreeWidget

拖放两个QTreeWidget

本文介绍了拖放两个QTreeWidget,根据之前设置的数据过滤QTreeWidgetItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目我有两个 qTreeWidgets(假设 A 和 B),我想将项目从 treeWidget A 拖到 treeWidget B.

Im my project I have two qTreeWidgets (let's say A and B), I want to drag Items from treeWidget A to treeWidget B.

我有多个问题:

并非所有存储在 A 中的项目都应该是 Draggale.在我代码的其他部分,我通过在构造函数中定义类型"来将它们分开:

Not all of the items stored in A should be draggale. At other parts of my code I seperate them by defining the "type" within the constructor:

 QTreeWidgetItem *newDatatype = new  QTreeWidgetItem(TreeWidgetItemTypes::Datatype);

我可以使用以下方法检查它们:

I can check them by using:

if(itemDroppedFromHeaven.type() == TreeWidgetItemTypes::Datatype) ...

因此我有一个枚举来分隔所有不同类型的项目.

Therefore I have a enum to seperate all the different kinds of Items.

  • 是否拖放复制设置的数据?
  • 如何在拖动时检测 Item 的类型?
  • 如何为启用/禁用某种项目设置拖动?

我试图重载 treeWidget B 的 dropEvent,以过滤掉的项目:

Iv'e tried to overload the dropEvent of the treeWidget B, to filter the dropped Items:

class CustomTreeWidget : public QTreeWidget
{
    Q_OBJECT

public:
    using QTreeWidget::QTreeWidget;
    void dropEvent(QDropEvent *event) override
    {
        QString format = event->mimeData()->formats().at(0);
        QString data = event->mimeData()->text();

        qDebug () << data;

        QStandardItem *sitem = new QStandardItem;

        QString itemName = sitem->text();
        qDebug() << itemName;

        if (itemName == "Messages" || itemName == "Datatypes" || itemName == "Enums")
        {
                event->setDropAction(Qt::IgnoreAction);
                return;
        }
        else
        {
            QTreeWidget::dropEvent(event);
            emit itemDropped();
        }
    }

signals:
    void itemDropped();
};

但是有了这个想法,我只能设置可见的项目名称:

But with this idea I can only get the visible Item Name set with:

 newDatatype->setData(0, Qt::ItemDataRole::DisplayRole, datName);

即使设置为用户角色的数据也不会被复制:

even data set as user role are not copied:

 newDatatype->setData(0, Qt::ItemDataRole::UserRole, datObjKey);

我感谢每一个想法!最好的

I'm thankful for every idea!Best

推荐答案

您不必覆盖任何内容,您只需使用以下命令停用标志 Qt::ItemIsDragEnabled:

You do not have to overwrite anything, you just have to deactivate the flag Qt::ItemIsDragEnabled with:

item->setFlags(item->flags() &= ~Qt::ItemIsDragEnabled);

示例:

#include <QtWidgets>

static void populate_treewidget(QTreeWidget *tree_widget){
    for(int i=0; i<5; ++i){
        QTreeWidgetItem *item = new QTreeWidgetItem(tree_widget);
        if(i%2 == 0){
            item->setText(0, "Not Dragable");
            item->setFlags(item->flags() &= ~Qt::ItemIsDragEnabled);
        }
        else
            item->setText(0, "Normal");
        for(int j=0; j<4; ++j){
            QTreeWidgetItem *child_item = new QTreeWidgetItem(item);
            if(j%2 == 1){
                child_item->setText(0, "Not Dragable");
                child_item->setFlags(child_item->flags() &= ~Qt::ItemIsDragEnabled);
            }
            else
                child_item->setText(0, "Normal");
        }
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QTreeWidget A;
    A.setDragEnabled(true);
    populate_treewidget(&A);
    QTreeWidget B;
    B.setAcceptDrops(true);
    QHBoxLayout *hlay = new QHBoxLayout(&w);
    hlay->addWidget(&A);
    hlay->addWidget(&B);
    A.expandAll();
    w.resize(640, 480);
    w.show();
    return a.exec();
}

这篇关于拖放两个QTreeWidget,根据之前设置的数据过滤QTreeWidgetItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:56