DEV Community

海前 王
海前 王

Posted on

treeview

#include <QApplication>
#include <QMainWindow>
#include <QTreeView>
#include <QStandardItemModel>
#include <QDebug>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QToolBar>
#include <QMenuBar>
#include <QAction>

class MyTreeView : public QTreeView {
    Q_OBJECT

public:
    MyTreeView(QWidget *parent = nullptr) : QTreeView(parent) {
        // 创建模型并设置数据
        model = new QStandardItemModel(5, 3, this); // 5 行 3 列
        setModel(model);

        // 填充数据
        for (int row = 0; row < 5; ++row) {
            for (int col = 0; col < 3; ++col) {
                QStandardItem *item = new QStandardItem(QString("(%1, %2)").arg(row).arg(col));
                model->setItem(row, col, item);
            }
        }

        // 设置树视图的一些特性
        setHeaderHidden(false);
        setColumnWidth(0, 150); // 默认列宽

        // 连接信号和槽
        connect(header(), &QHeaderView::sectionResized, this, &MyTreeView::columnResized);
        connect(header(), &QHeaderView::sectionMoved, this, &MyTreeView::columnMoved);
        connect(model, &QStandardItemModel::rowsRemoved, this, &MyTreeView::rowsRemoved);
    }

public Q_SLOTS:
    void hideColumn(int column) {
        setColumnHidden(column, true);
        qDebug() << "Column" << column << "hidden.";
    }

    void showColumn(int column) {
        setColumnHidden(column, false);
        qDebug() << "Column" << column << "shown.";
    }

    void expand(const QModelIndex &index) {
        expand(index);
        emit expanded(index);
        qDebug() << "Node expanded:" << index.data().toString();
    }

    void collapse(const QModelIndex &index) {
        collapse(index);
        emit collapsed(index);
        qDebug() << "Node collapsed:" << index.data().toString();
    }

    void resizeColumnToContents(int column) {
        resizeColumnToContents(column);
        qDebug() << "Column" << column << "resized to contents.";
    }

    void sortByColumn(int column) {
        sortByColumn(column, Qt::AscendingOrder);
        qDebug() << "Sorted by column" << column;
    }

    void expandAll() {
        expandAll();
        qDebug() << "All nodes expanded.";
    }

    void collapseAll() {
        collapseAll();
        qDebug() << "All nodes collapsed.";
    }

    void expandToDepth(int depth) {
        expandToDepth(depth);
        qDebug() << "Expanded to depth" << depth;
    }

signals:
    void expanded(const QModelIndex &index);
    void collapsed(const QModelIndex &index);

protected Q_SLOTS:
    void columnResized(int column, int oldSize, int newSize) {
        qDebug() << "Column" << column << "resized from" << oldSize << "to" << newSize;
    }

    void columnCountChanged(int oldCount, int newCount) {
        qDebug() << "Column count changed from" << oldCount << "to" << newCount;
    }

    void columnMoved() {
        qDebug() << "Column moved.";
    }

    void reexpand() {
        qDebug() << "Reexpanding nodes.";
        // 实现重新展开的逻辑
    }

    void rowsRemoved(const QModelIndex &parent, int first, int last) {
        qDebug() << "Rows removed from" << first << "to" << last;
    }

private:
    QStandardItemModel *model;
};

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        treeView = new MyTreeView(this);
        setCentralWidget(treeView);

        // 创建工具栏和动作
        QToolBar *toolBar = addToolBar("Main Toolbar");

        QAction *hideColumnAction = new QAction("Hide Column 1", this);
        QAction *showColumnAction = new QAction("Show Column 1", this);
        QAction *expandAction = new QAction("Expand Node", this);
        QAction *collapseAction = new QAction("Collapse Node", this);
        QAction *resizeColumnAction = new QAction("Resize Column 1", this);
        QAction *sortByColumnAction = new QAction("Sort by Column 1", this);
        QAction *expandAllAction = new QAction("Expand All", this);
        QAction *collapseAllAction = new QAction("Collapse All", this);
        QAction *expandToDepthAction = new QAction("Expand to Depth 2", this);

        connect(hideColumnAction, &QAction::triggered, this, &MainWindow::hideColumn);
        connect(showColumnAction, &QAction::triggered, this, &MainWindow::showColumn);
        connect(expandAction, &QAction::triggered, this, &MainWindow::expand);
        connect(collapseAction, &QAction::triggered, this, &MainWindow::collapse);
        connect(resizeColumnAction, &QAction::triggered, this, &MainWindow::resizeColumn);
        connect(sortByColumnAction, &QAction::triggered, this, &MainWindow::sortByColumn);
        connect(expandAllAction, &QAction::triggered, this, &MainWindow::expandAll);
        connect(collapseAllAction, &QAction::triggered, this, &MainWindow::collapseAll);
        connect(expandToDepthAction, &QAction::triggered, this, &MainWindow::expandToDepth);

        toolBar->addAction(hideColumnAction);
        toolBar->addAction(showColumnAction);
        toolBar->addAction(expandAction);
        toolBar->addAction(collapseAction);
        toolBar->addAction(resizeColumnAction);
        toolBar->addAction(sortByColumnAction);
        toolBar->addAction(expandAllAction);
        toolBar->addAction(collapseAllAction);
        toolBar->addAction(expandToDepthAction);

        // 创建菜单栏
        QMenu *editMenu = menuBar()->addMenu("Edit");
        editMenu->addAction(hideColumnAction);
        editMenu->addAction(showColumnAction);
        editMenu->addAction(expandAction);
        editMenu->addAction(collapseAction);
        editMenu->addAction(resizeColumnAction);
        editMenu->addAction(sortByColumnAction);
        editMenu->addAction(expandAllAction);
        editMenu->addAction(collapseAllAction);
        editMenu->addAction(expandToDepthAction);
    }

private Q_SLOTS:
    void hideColumn() {
        treeView->hideColumn(1); // Hide column 1
    }

    void showColumn() {
        treeView->showColumn(1); // Show column 1
    }

    void expand() {
        QModelIndex index = treeView->currentIndex(); // Use the currently selected index
        treeView->expand(index);
    }

    void collapse() {
        QModelIndex index = treeView->currentIndex(); // Use the currently selected index
        treeView->collapse(index);
    }

    void resizeColumn() {
        treeView->resizeColumnToContents(1); // Resize column 1
    }

    void sortByColumn() {
        treeView->sortByColumn(1); // Sort by column 1
    }

    void expandAll() {
        treeView->expandAll();
    }

    void collapseAll() {
        treeView->collapseAll();
    }

    void expandToDepth() {
        treeView->expandToDepth(2); // Expand to depth 2
    }

private:
    MyTreeView *treeView;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWindow window;
    window.resize(800, 600);
    window.show();

    return app.exec();
}

#include "main.moc"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)