C++ QT defined a CustomTreeView, 可选择多个node,并且可拖动这些node,改变父节点

To create a custom QTreeView in C++ using Qt, where users can select multiple nodes and drag them to change their parent nodes, you'll need to leverage Qt's model-view framework along with the drag-and-drop feature. Below is a step-by-step guide to achieving this.

Step 1: Subclass QTreeView and QAbstractItemModel

You'll need to subclass both QTreeView (the view) and QAbstractItemModel (the model). The QTreeView will handle the user interface, and the QAbstractItemModel will manage the data.

Step 2: Create the Custom QTreeView

The custom QTreeView will allow multiple selections and handle drag-and-drop actions.

  1. Subclass QTreeView to enable multi-selection and drag-and-drop:
复制代码

cpp

复制代码
`#include <QTreeView>
#include <QMimeData>
#include <QDrag>
#include <QDropEvent>
#include <QStandardItemModel>
#include <QStandardItem>

class CustomTreeView : public QTreeView {
    Q_OBJECT

public:
    CustomTreeView(QWidget *parent = nullptr) : QTreeView(parent) {
        setSelectionMode(QAbstractItemView::ExtendedSelection); // Allow multi-selection
        setDragDropMode(QAbstractItemView::InternalMove); // Enable drag-and-drop
        setDropIndicatorShown(true); // Show drop indicator
    }

    void dropEvent(QDropEvent *event) override {
        // Handle drop event here if needed, like checking validity of drop
        QTreeView::dropEvent(event);
    }
};
`

cpp

Step 3: Create the Model (Subclass QAbstractItemModel)

Next, subclass QAbstractItemModel to represent your data structure. Here's an example of how you might implement it:

复制代码

cpp

复制代码
`#include <QAbstractItemModel>
#include <QVector>
#include <QVariant>

class CustomTreeModel : public QAbstractItemModel {
    Q_OBJECT

public:
    CustomTreeModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {
        // Sample data: (root, child1, child2)
        rootItem = new QStandardItem("Root");
        QStandardItem *child1 = new QStandardItem("Child 1");
        QStandardItem *child2 = new QStandardItem("Child 2");
        rootItem->appendRow(child1);
        rootItem->appendRow(child2);
    }

    ~CustomTreeModel() {
        delete rootItem;
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const override {
        if (!parent.isValid()) {
            return rootItem->rowCount();
        }
        QStandardItem *parentItem = static_cast<QStandardItem*>(parent.internalPointer());
        return parentItem->rowCount();
    }

    int columnCount(const QModelIndex &parent = QModelIndex()) const override {
        return parent.isValid() ? static_cast<QStandardItem*>(parent.internalPointer())->columnCount() : 1;
    }

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
        if (role == Qt::DisplayRole) {
            QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
            return item->text();
        }
        return QVariant();
    }

    Qt::ItemFlags flags(const QModelIndex &index) const override {
        Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
        return defaultFlags | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
    }

    QModelIndex parent(const QModelIndex &index) const override {
        if (!index.isValid()) {
            return QModelIndex();
        }

        QStandardItem *childItem = static_cast<QStandardItem*>(index.internalPointer());
        QStandardItem *parentItem = childItem->parent();
        if (parentItem == rootItem) {
            return QModelIndex(); // Root node has no parent
        }
        return createIndex(parentItem->row(), 0, parentItem);
    }

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override {
        if (!parent.isValid()) {
            return createIndex(row, column, rootItem->child(row));
        }
        QStandardItem *parentItem = static_cast<QStandardItem*>(parent.internalPointer());
        return createIndex(row, column, parentItem->child(row));
    }

private:
    QStandardItem *rootItem;
};
`

cpp

Step 4: Integrating Model and View

Now that you have the custom view (CustomTreeView) and model (CustomTreeModel), set them up in your MainWindow or wherever you want to display the tree.

复制代码

cpp

复制代码
`#include <QMainWindow>
#include <QTreeView>
#include <QStandardItemModel>

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        // Create the custom tree view
        CustomTreeView *treeView = new CustomTreeView(this);
        treeView->setModel(new CustomTreeModel(this)); // Set the custom model
        setCentralWidget(treeView);
    }
};

`

cpp

Step 5: Enable Dragging and Dropping Logic

To support dragging and dropping, the CustomTreeView handles the dragMoveEvent, dragEnterEvent, and dropEvent in the dropEvent method. You may need to add additional logic to handle the rearrangement of nodes.

Step 6: Running the Application

Now, set up the application entry point:

复制代码

cpp

复制代码
`#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
`

Summary:

  • QTreeView: Subclassed to handle multi-selection and drag-and-drop functionality.
  • QAbstractItemModel: Subclassed to represent the tree data structure.
  • Drag and Drop : Supported by setting the appropriate flags and handling the events in CustomTreeView.

By subclassing QTreeView and QAbstractItemModel, you create a flexible, custom tree view that allows for multi-selection and drag-and-drop functionality. You can further extend the logic to handle additional features like validation, custom item rendering, etc.

//

//

split to .h and .cpp

Sure! Here's the code split into header (.h) and implementation (.cpp) files for a better structure.

CustomTreeView.h

复制代码

cpp

复制代码
`#ifndef CUSTOMTREEVIEW_H
#define CUSTOMTREEVIEW_H

#include <QTreeView>
#include <QDropEvent>

class CustomTreeView : public QTreeView {
    Q_OBJECT

public:
    explicit CustomTreeView(QWidget *parent = nullptr);

protected:
    void dropEvent(QDropEvent *event) override;
};

#endif // CUSTOMTREEVIEW_H
`

cpp

CustomTreeView.cpp

复制代码

cpp

复制代码
`#include "CustomTreeView.h"
#include <QMimeData>
#include <QDrag>

CustomTreeView::CustomTreeView(QWidget *parent) : QTreeView(parent) {
    setSelectionMode(QAbstractItemView::ExtendedSelection);  // Allow multi-selection
    setDragDropMode(QAbstractItemView::InternalMove);        // Enable drag-and-drop
    setDropIndicatorShown(true);                              // Show drop indicator
}

void CustomTreeView::dropEvent(QDropEvent *event) {
    // Handle drop event here if needed (e.g., validate drop)
    QTreeView::dropEvent(event);
}
`

cpp

CustomTreeModel.h

复制代码

cpp

复制代码
`#ifndef CUSTOMTREEMODEL_H
#define CUSTOMTREEMODEL_H

#include <QAbstractItemModel>
#include <QStandardItem>

class CustomTreeModel : public QAbstractItemModel {
    Q_OBJECT

public:
    explicit CustomTreeModel(QObject *parent = nullptr);
    ~CustomTreeModel();

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;

private:
    QStandardItem *rootItem;
};

#endif // CUSTOMTREEMODEL_H
`

cpp

CustomTreeModel.cpp

复制代码

cpp

复制代码
`#include "CustomTreeModel.h"

CustomTreeModel::CustomTreeModel(QObject *parent) : QAbstractItemModel(parent) {
    // Sample data: (root, child1, child2)
    rootItem = new QStandardItem("Root");
    QStandardItem *child1 = new QStandardItem("Child 1");
    QStandardItem *child2 = new QStandardItem("Child 2");
    rootItem->appendRow(child1);
    rootItem->appendRow(child2);
}

CustomTreeModel::~CustomTreeModel() {
    delete rootItem;
}

int CustomTreeModel::rowCount(const QModelIndex &parent) const {
    if (!parent.isValid()) {
        return rootItem->rowCount();
    }
    QStandardItem *parentItem = static_cast<QStandardItem*>(parent.internalPointer());
    return parentItem->rowCount();
}

int CustomTreeModel::columnCount(const QModelIndex &parent) const {
    return parent.isValid() ? static_cast<QStandardItem*>(parent.internalPointer())->columnCount() : 1;
}

QVariant CustomTreeModel::data(const QModelIndex &index, int role) const {
    if (role == Qt::DisplayRole) {
        QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
        return item->text();
    }
    return QVariant();
}

Qt::ItemFlags CustomTreeModel::flags(const QModelIndex &index) const {
    Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
    return defaultFlags | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}

QModelIndex CustomTreeModel::parent(const QModelIndex &index) const {
    if (!index.isValid()) {
        return QModelIndex();
    }

    QStandardItem *childItem = static_cast<QStandardItem*>(index.internalPointer());
    QStandardItem *parentItem = childItem->parent();
    if (parentItem == rootItem) {
        return QModelIndex();  // Root node has no parent
    }
    return createIndex(parentItem->row(), 0, parentItem);
}

QModelIndex CustomTreeModel::index(int row, int column, const QModelIndex &parent) const {
    if (!parent.isValid()) {
        return createIndex(row, column, rootItem->child(row));
    }
    QStandardItem *parentItem = static_cast<QStandardItem*>(parent.internalPointer());
    return createIndex(row, column, parentItem->child(row));
}
`

cpp

MainWindow.h

复制代码

cpp

复制代码
`#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class CustomTreeView;
class CustomTreeModel;

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow() override;

private:
    CustomTreeView *treeView;
    CustomTreeModel *model;
};

#endif // MAINWINDOW_H
`

cpp

MainWindow.cpp

复制代码

cpp

复制代码
`#include "MainWindow.h"
#include "CustomTreeView.h"
#include "CustomTreeModel.h"

#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      treeView(new CustomTreeView(this)),
      model(new CustomTreeModel(this)) {
    
    treeView->setModel(model);  // Set the custom model to the tree view

    // Set up the main window layout
    setCentralWidget(treeView);
}

MainWindow::~MainWindow() {
    // Any cleanup if necessary (not strictly needed as Qt handles it)
}
`

cpp

main.cpp

复制代码

cpp

复制代码
`#include <QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
`

Explanation:

  1. CustomTreeView.h & CustomTreeView.cpp:

    • The view class (CustomTreeView) is set up to allow multi-selection and internal drag-and-drop. The dropEvent method is overridden to handle drops (if needed).
  2. CustomTreeModel.h & CustomTreeModel.cpp:

    • The model class (CustomTreeModel) is a subclass of QAbstractItemModel that provides data for the tree. It manages a root item and its child nodes.
  3. MainWindow.h & MainWindow.cpp:

    • This class initializes the application window and connects the custom tree view with the model.
  4. main.cpp:

    • This file sets up the Qt application and shows the main window.

Compiling and Running:

  • Include the necessary Qt modules (e.g., QtCore, QtGui, QtWidgets) in your .pro file or CMake setup.

  • If you use a .pro file, it might look like this:

    复制代码
    QT += core gui widgets
    SOURCES += main.cpp MainWindow.cpp CustomTreeView.cpp CustomTreeModel.cpp
    HEADERS += MainWindow.h CustomTreeView.h CustomTreeModel.h

Now you have a well-structured Qt application that uses a custom tree view with drag-and-drop functionality. Let me know if you need further details or have questions about customization!

相关推荐
卡尔特斯2 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
白鲸开源2 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
ytadpole3 小时前
Java 25 新特性 更简洁、更高效、更现代
java·后端
感哥3 小时前
C++ 面向对象
c++
纪莫3 小时前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
JavaGuide4 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户3721574261354 小时前
Java 轻松批量替换 Word 文档文字内容
java
白鲸开源4 小时前
教你数分钟内创建并运行一个 DolphinScheduler Workflow!
java
沐怡旸4 小时前
【底层机制】std::shared_ptr解决的痛点?是什么?如何实现?如何正确用?
c++·面试
Java中文社群4 小时前
有点意思!Java8后最有用新特性排行榜!
java·后端·面试