QT实现树形结构

Qt中实现树形结构可以使用QTreeWidget类,也可以使用QTreeView类,QTreeWidget继承自QTreeView类。树形效果如下图所示

ui->treeWidget->clear();    //QTreeWidget清空
ui->treeWidget->setFrameStyle(QFrame::NoFrame);  //框架样式
ui->treeWidget->setHeaderLabel("tree widget");  //设置头的标题
ui->treeWidget->setHeaderHidden(true);     //tree widget头标题是否显示,此处隐藏标题
ui->treeWidget->setColumnCount(1);    //tree widget展示的列数
Global_item->setIcon(0, QIcon(":/new/prefix1/image/begintesting.png")); //为节点添加图标
ui->treeWidget->setHeaderLabels(QStringList()<<"部门");
    QTreeWidgetItem * item =new QTreeWidgetItem(QStringList()<<"研发部");
    QTreeWidgetItem * item1 =new QTreeWidgetItem(QStringList()<<"行政部");
    QTreeWidgetItem * item2 =new QTreeWidgetItem(QStringList()<<"财务部");
    QTreeWidgetItem * item3 =new QTreeWidgetItem(QStringList()<<"研发部");
    //加载顶层的节点
    ui->treeWidget->addTopLevelItem(item);
    ui->treeWidget->addTopLevelItem(item1);
    ui->treeWidget->addTopLevelItem(item2);
    //追加子节点
    QStringList herol1;
    herol1<<"前端";
    QTreeWidgetItem * xy=new QTreeWidgetItem(herol1);
    item->addChild(xy);

    QStringList herolx;
    herol1<<"ui";
    QTreeWidgetItem * xyx=new QTreeWidgetItem(herol1);
    xy->addChild(xyx);

    QStringList herol2;
    herol2<<"xxx";
    QTreeWidgetItem * xy1=new QTreeWidgetItem(herol2);
    item->addChild(xy1);

    QStringList herol3;
    herol3<<"xxx ";
    QTreeWidgetItem * xy2=new QTreeWidgetItem(herol3);
    item1->addChild(xy2);

    QStringList herol4;
    herol4<<"xxx ";
    QTreeWidgetItem * xy3=new QTreeWidgetItem(herol4);
    item1->addChild(xy3);

    QStringList herol5;
    herol5<<"xxx ";
    QTreeWidgetItem * xy4=new QTreeWidgetItem(herol5);
    item2->addChild(xy4);

    QStringList herol6;
    herol6<<"xxx";
    QTreeWidgetItem * xy5=new QTreeWidgetItem(herol6);
    item2->addChild(xy5);

    QTreeWidgetItem *topItem2 = new QTreeWidgetItem(ui->treeWidget);
    topItem2->setText(0,"腾讯");
    topItem2->setCheckState(0,Qt::Checked);  //添加选中标志 unchecked (未选中标志)
    ui->treeWidget->addTopLevelItem(topItem2);

//当树形结构添加checkBox后,可以自定义槽函数,实现当顶层节点选中,下面的所有子节点全选中。connect函数:
connect(ui->treeWidget,&QTreeWidget::itemClicked,this,&myMainWindow::treeWidgetClicked);
void myMainWindow::treeWidgetClicked(QTreeWidgetItem *item)
{
    for (int i=0; i<item->childCount(); i++)
    {
       QTreeWidgetItem *pChildItem = item->child(i);
       pChildItem->setCheckState(0,item->checkState(0));
    }
}
//第一种加菜单的方式
       ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);  //使treewidget支持右击菜单
       //右键菜单
       m_menu= new QMenu(ui->treeWidget);
       m_menu->addAction("添加");
       m_menu->addAction("删除");
              //响应右键菜单信号槽
       connect(ui->treeWidget,&QTreeWidget::customContextMenuRequested,
               this,&serviceForm::slotMenuPopup);
void serviceForm::slotMenuPopup(const QPoint &pos)
{
    m_menu->exec(ui->treeWidget->mapToGlobal(pos));  //弹出菜单的位置  mapToGlobal 将小部件的坐标转换成全局坐标
}

QTreeWidgetItem* item = new QTreeWidgetItem(ui.treeWidget);
 
/// 设置某一行的内容可编辑
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable); 
       ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);  //使treewidget支持右击菜单
       connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested,
               this, &serviceForm::SlotCustomContextMenuRequested);//实现右击添加菜单的信号与槽的连接
void serviceForm::SlotCustomContextMenuRequested(const QPoint& pos)
{
    QAction*m_pact = new QAction(QStringLiteral("删除"), this);
    //QAction *pDelAction_1 = new QAction(QStringLiteral("重命名"), this);
    QMenu *popMenu = new QMenu(this);//定义右键弹出菜单
    popMenu->addAction(m_pact);//往菜单内添加QAction
    // QAction *deleAction = cmenu->addAction("Delete");//另一种创建方式
//    connect(m_pact, SIGNAL(triggered()), this, SLOT(DeleteItemFunction()));
    connect(m_pact,&QAction::triggered,this,&serviceForm::DeleteItemFunction);
    popMenu->exec(QCursor::pos());//弹出右键菜单,菜单位置为光标位置
}
void serviceForm::DeleteItemFunction()
{
    QTreeWidgetItem * currentItem = ui->treeWidget->currentItem();//获取当前节点
    if (currentItem == NULL)
    {
        return;
    }
    else
        delete currentItem;
}
    ui->treeWidget->setSelectionBehavior(QAbstractItemView::SelectRows);    // 整行选中
       ui->treeWidget->setMouseTracking(true); // 启动鼠标跟踪
       ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); //使treewidget支持右击菜单
       //隐藏表头
       ui->treeWidget->setHeaderHidden(true);
       //设置展开
//       ui->treeWidget->expandAll();

       //ui.menu_listWidget->setEditTriggers(QAbstractItemView::DoubleClicked);
       m_menu = new QMenu;

       connect(ui->treeWidget, &QWidget::customContextMenuRequested, this, &serviceForm::SlotMenuListWidgetRightClicked);
       connect(m_menu, &QMenu::triggered, this, &serviceForm::SlotGroupMenuClicked);
void serviceForm::SlotMenuListWidgetRightClicked(const QPoint &pos)
{
    m_menu->clear();
        QModelIndex index = ui->treeWidget->indexAt(pos);
        int row = index.row();
        QString groupName;
//        if (row < 0)
//            groupName = "";
//        else

//            groupName = ui->treeWidget->item(row)->text();
//            groupName = ui->treeWidget->item
        QAction* newAction = new QAction(m_menu);
        newAction->setText("新增分组");
        m_menu->addAction(newAction);
        if (groupName != "") {
            QAction* deleteAction = new QAction(m_menu);
            deleteAction->setText("删除分组");
            m_menu->addAction(deleteAction);
        }
        m_menu->exec(cursor().pos());

}
void serviceForm::SlotGroupMenuClicked(QAction* action)
{
    if (action->text() == "新增分组") {
            int rowCount = ui->treeWidget->topLevelItemCount();
            QTreeWidgetItem* itemxx = new QTreeWidgetItem(QStringList("请设置分组名称"));
            itemxx->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
            ui->treeWidget->insertTopLevelItem(rowCount, itemxx);
        }
        else if (action->text() == QStringLiteral("删除分组")) {
            QTreeWidgetItem * currentItem = ui->treeWidget->currentItem();//获取当前节点
            delete currentItem;
        }
}

你可能感兴趣的:(QT,C++,qt,ui,开发语言)