Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TreeView getting all children in a TreeItemModel is empty
Forum Updated to NodeBB v4.3 + New Features

TreeView getting all children in a TreeItemModel is empty

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DJViking
    wrote on last edited by
    #1

    I am new to Qt. Trying to learn. I am using Qt 5, since it comes with my Linux distribution KDE, I wanted to use that version instead of Qt 6.

    I am having some trouble with TreeView and TreeItemModel.

    I am using the QStandardItemModel and QStandardItem classes.
    Though I have made my own subclasses of them.

    class SubsystemItemModel : public QStandardItemModel
    class SubsystemItem : public QStandardItem
    

    I did try to follow the Simple Tree Model Example, but found it a bit complex. I found using QAbstractItemModel was a bit to complex for my level of Qt understanding. To much I had to implement on my own, and I did not need multiple columns in my TreeView data.
    https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

    I am trying to get all the children in the model, all the items.
    I have tried getting them by calling both of these two methods on the QStandardItemModel
    https://doc.qt.io/qt-5/qobject.html#children
    https://doc.qt.io/qt-5/qobject.html#findChildren

    However both lists are empty.

        SubsystemItemModel subsystem_model = new SubsystemItemModel(mw, this);
        subsystem_model->setHorizontalHeaderLabels(QStringList() << "Subsystems");
    
        QTreeView *treeView = ui->subsystemTreeView;
        treeView->setModel(subsystem_model);
    
        SubsystemItem *item0 = new SubsystemItem("TEST");
        subsystem_model->appendRow(item0);
    
        SubsystemItem *item01 = new SubsystemItem("TEST.SUBTEST_1");
        item0->appendRow(item01);
        SubsystemItem *item02 = new SubsystemItem("TEST.SUBTEST_2");
        item0->appendRow(item02);
        SubsystemItem *item03 = new SubsystemItem("TEST.SUBTEST_3");
        item0->appendRow(item03);
        SubsystemItem *item04 = new SubsystemItem("TEST.SUBTEST_4");
        item0->appendRow(item04);
        SubsystemItem *item05 = new SubsystemItem("TEST.SUBTEST_5");
        item0->appendRow(item05);
    
        std::cout << "children" << std::endl;
        QObjectList children = subsystem_model->children();
        foreach (auto obj, children) {
          std::cout << "child " << obj->objectName().toStdString() << std::endl;
        }
    
        std::cout << "findChildren" << std::endl;
        QList<QObject*> list = subsystem_model->findChildren<QObject*>();
        foreach (auto obj, children) {
          std::cout << "child " << obj->objectName().toStdString() << std::endl;
        }
    

    The items are shown in the TreeView when I run my application, but both these lists are empty.

    JonBJ 1 Reply Last reply
    0
    • D DJViking

      I am new to Qt. Trying to learn. I am using Qt 5, since it comes with my Linux distribution KDE, I wanted to use that version instead of Qt 6.

      I am having some trouble with TreeView and TreeItemModel.

      I am using the QStandardItemModel and QStandardItem classes.
      Though I have made my own subclasses of them.

      class SubsystemItemModel : public QStandardItemModel
      class SubsystemItem : public QStandardItem
      

      I did try to follow the Simple Tree Model Example, but found it a bit complex. I found using QAbstractItemModel was a bit to complex for my level of Qt understanding. To much I had to implement on my own, and I did not need multiple columns in my TreeView data.
      https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

      I am trying to get all the children in the model, all the items.
      I have tried getting them by calling both of these two methods on the QStandardItemModel
      https://doc.qt.io/qt-5/qobject.html#children
      https://doc.qt.io/qt-5/qobject.html#findChildren

      However both lists are empty.

          SubsystemItemModel subsystem_model = new SubsystemItemModel(mw, this);
          subsystem_model->setHorizontalHeaderLabels(QStringList() << "Subsystems");
      
          QTreeView *treeView = ui->subsystemTreeView;
          treeView->setModel(subsystem_model);
      
          SubsystemItem *item0 = new SubsystemItem("TEST");
          subsystem_model->appendRow(item0);
      
          SubsystemItem *item01 = new SubsystemItem("TEST.SUBTEST_1");
          item0->appendRow(item01);
          SubsystemItem *item02 = new SubsystemItem("TEST.SUBTEST_2");
          item0->appendRow(item02);
          SubsystemItem *item03 = new SubsystemItem("TEST.SUBTEST_3");
          item0->appendRow(item03);
          SubsystemItem *item04 = new SubsystemItem("TEST.SUBTEST_4");
          item0->appendRow(item04);
          SubsystemItem *item05 = new SubsystemItem("TEST.SUBTEST_5");
          item0->appendRow(item05);
      
          std::cout << "children" << std::endl;
          QObjectList children = subsystem_model->children();
          foreach (auto obj, children) {
            std::cout << "child " << obj->objectName().toStdString() << std::endl;
          }
      
          std::cout << "findChildren" << std::endl;
          QList<QObject*> list = subsystem_model->findChildren<QObject*>();
          foreach (auto obj, children) {
            std::cout << "child " << obj->objectName().toStdString() << std::endl;
          }
      

      The items are shown in the TreeView when I run my application, but both these lists are empty.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Since QStandardItem is not derived from QObject neither children() nor qFindChild() can return them.
        You're looking for invisibleRootItem() where you then can traverse through all the child items.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          Since QStandardItem is not derived from QObject neither children() nor qFindChild() can return them.
          You're looking for invisibleRootItem() where you then can traverse through all the child items.

          D Offline
          D Offline
          DJViking
          wrote on last edited by DJViking
          #4

          @Christian-Ehrlicher
          I thought so too. I was checking into using the invisibleRootItem(), but cannot find which method to call upon.
          https://doc.qt.io/qt-5/qstandarditem.html

          I tried this, but I am not getting all the children.

              QStandardItem *rootItem = subsystem_model->invisibleRootItem();
              auto rowCount = rootItem->rowCount();
              std::cout << "Rows " << std::to_string(rowCount) << std::endl;
              for (int row = 0; row < rowCount; ++row)
              {
                  QStandardItem *item = rootItem->child(row);
                  std::cout << item->text().toStdString() << std::endl;
              }
          

          The rowCount=1. I guess that is right, considering I only have one row added on the model. Then the first row has 5 items.
          Do I need to recursive traverse my self to get all items?

          Christian EhrlicherC 1 Reply Last reply
          0
          • D DJViking

            @Christian-Ehrlicher
            I thought so too. I was checking into using the invisibleRootItem(), but cannot find which method to call upon.
            https://doc.qt.io/qt-5/qstandarditem.html

            I tried this, but I am not getting all the children.

                QStandardItem *rootItem = subsystem_model->invisibleRootItem();
                auto rowCount = rootItem->rowCount();
                std::cout << "Rows " << std::to_string(rowCount) << std::endl;
                for (int row = 0; row < rowCount; ++row)
                {
                    QStandardItem *item = rootItem->child(row);
                    std::cout << item->text().toStdString() << std::endl;
                }
            

            The rowCount=1. I guess that is right, considering I only have one row added on the model. Then the first row has 5 items.
            Do I need to recursive traverse my self to get all items?

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @DJViking said in TreeView getting all children in a TreeItemModel is empty:

            Do I need to recursive traverse my self to get all items?

            Yes, it's a tree.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved