qq_43680827
太阳风暴
2020-06-11 09:46

QTreeWidget添加多个节点只能显示一个

  • c++

QTreeWidget添加了多个子节点,只能显示其中一个子节点,我不知道是不是插入部分有问题

 QTreeWidgetItem *Top1Item = new QTreeWidgetItem();
    QTreeWidgetItem *Top2Item = new QTreeWidgetItem;
    QTreeWidgetItem *Top3Item = new QTreeWidgetItem;

    Top1Item->setText(0, tr("1级菜单1"));
    Top2Item->setText(0, tr("1级菜单2"));
    Top3Item->setText(0, tr("1级菜单3"));

    QList <QTreeWidgetItem *> TopLists;
    TopLists<<Top1Item<<Top2Item<<Top3Item;

    QTreeWidgetItem *SecondItem1 = new QTreeWidgetItem;
    QTreeWidgetItem *SecondItem2 = new QTreeWidgetItem;
    QTreeWidgetItem *SecondItem3 = new QTreeWidgetItem;
    QTreeWidgetItem *SecondItem4 = new QTreeWidgetItem;

    SecondItem1->setText(0, "二级菜单1");
    SecondItem2->setText(0, "二级菜单2");
    SecondItem3->setText(0, "二级菜单3");
    SecondItem4->setText(0, "二级菜单4");

    QList <QTreeWidgetItem *> SecondLists;
    SecondLists<<SecondItem1<<SecondItem2<<SecondItem3<<SecondItem4;

    QTreeWidgetItem *ThirdItem1 = new QTreeWidgetItem;
    QTreeWidgetItem *ThirdItem2 = new QTreeWidgetItem;
    QTreeWidgetItem *ThirdItem3 = new QTreeWidgetItem;

    ThirdItem1->setText(0, "三级菜单1");
    ThirdItem2->setText(0, "三级菜单2");
    ThirdItem3->setText(0, "三级菜单3");
    QList <QTreeWidgetItem *> ThirdLists;
    ThirdLists<<ThirdItem1<<ThirdItem2<<ThirdItem3;

    QTreeWidgetItem *Item = new QTreeWidgetItem;
    Item->setText(0, "子项");
    qDebug()<<"TopLists.size: "<<TopLists.size()<<"SecondLists.size: "<<SecondLists.size()<<"ThirdLists.size: "<<ThirdLists.size();

//插入节点
    for (int i = 1;i < TopLists.size();i++) {
        TopLists[i]->addChildren(SecondLists);
        for (int j = 0;j < SecondLists.size();j++) {
            SecondLists[i]->addChildren(ThirdLists);
            for (int k= 0;k < ThirdLists.size();k++) {
                ThirdLists[i]->addChild(Item);
            }
        }
    }

    ui->treeWidget->insertTopLevelItems(0, TopLists);

运行结果如下图

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答