太阳风暴
2020-06-11 09:46
采纳率: 0%
浏览 496

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

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条回答 默认 最新

  • qtchen_1988 2020-06-11 18:37
    
    //插入节点
        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);
                }
            }
        }
    
            ====>
    
        //插入节点    
        for (int k= 0;k < ThirdLists.size();k++) {
            ThirdLists[i]->addChild(Item);
        }
        for (int j = 0;j < SecondLists.size();j++) {
            SecondLists[i]->addChildren(ThirdLists);
        }
        for (int i = 1;i < TopLists.size();i++) {
            TopLists[i]->addChildren(SecondLists);
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题