'Why subchild appends only to first item?

Expected result:

  • parent
    • child1
      • subchild1
      • subchild2
      • subchild3
    • child2
      • subchild1
      • subchild2
      • subchild3
    • child3
      • subchild1
      • subchild2
      • subchild3

Code:

QTreeWidget *tree = ui->dbTree;
QTreeWidgetItem *dbName = new QTreeWidgetItem();
dbName->setText(0, "db_name");
tree->addTopLevelItem(dbName);

QList<QTreeWidgetItem*> childList;

QSqlQuery query(db);
query.exec("SELECT n.nspname FROM pg_catalog.pg_namespace n WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema';");

QList<QTreeWidgetItem *> subchildList;
QTreeWidgetItem *tb = new QTreeWidgetItem();
QTreeWidgetItem *vw = new QTreeWidgetItem();
QTreeWidgetItem *fc = new QTreeWidgetItem();
tb->setText(0, "Tables");
vw->setText(0, "Views");
fc->setText(0, "Functions");
subchildList.append(tb);
subchildList.append(vw);
subchildList.append(fc);

while (query.next()) {
    QTreeWidgetItem *child = new QTreeWidgetItem();
    child->setText(0, query.value(0).toString());
    child->addChildren(subchildList);

    childList.append(child);
}

QList<QTreeWidgetItem*> chs = ui->dbTree->findItems(QStringLiteral("db_name"), Qt::MatchExactly);
chs.at(0)->addChildren(childList);

Result I get:

  • parent
    • child1
      • subchild1
      • subchild2
      • subchild3
    • child2
    • child3

Any help would be appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source