expandall collapseall

海前 王 - Sep 2 - - Dev Community
#include <QApplication>
#include <QMainWindow>
#include <QTreeView>
#include <QStandardItemModel>
#include <QDebug>
#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(this);
        setModel(model);

        // 填充数据
        QStandardItem *rootItem = model->invisibleRootItem(); // 根节点

        for (int row = 0; row < 5; ++row) {
            QStandardItem *parentItem = new QStandardItem(QString("Parent %1").arg(row));
            rootItem->appendRow(parentItem);

            for (int col = 0; col < 3; ++col) {
                QStandardItem *childItem = new QStandardItem(QString("Child %1-%2").arg(row).arg(col));
                parentItem->appendRow(childItem);
            }
        }

        // 设置树视图的一些特性
        setHeaderHidden(false); // 显示列标题

        // 连接信号和槽
        connect(this, &QTreeView::collapsed, this, &MyTreeView::onNodeCollapsed);
        connect(this, &QTreeView::expanded, this, &MyTreeView::onNodeExpanded);
    }

public Q_SLOTS:
    void onNodeCollapsed(const QModelIndex &index) {
        qDebug() << "Node collapsed:" << index.data().toString();
    }

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

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

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

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 *expandAllAction = new QAction("Expand All", this);
        QAction *collapseAllAction = new QAction("Collapse All", this);

        connect(expandAllAction, &QAction::triggered, treeView, &MyTreeView::expandAllNodes);
        connect(collapseAllAction, &QAction::triggered, treeView, &MyTreeView::collapseAll);

        toolBar->addAction(expandAllAction);
        toolBar->addAction(collapseAllAction);

        // 创建菜单栏
        QMenu *editMenu = menuBar()->addMenu("Edit");
        editMenu->addAction(expandAllAction);
        editMenu->addAction(collapseAllAction);
    }

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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player