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

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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 12月1日

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码