QTreeView set index and make selected



  • Hi all I am new to Qt development my question may be silly kindly bear with me.
    I have implemented an editable qtreeview with example provided
    http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
    My tree looks like this.
    -User //*Parent
    .....+UserDept1 //*child
    ....+UserDept2 //*child
    ....-USerDept3 //*child
    ........... userName1//*Grandchild
    ........... username2//*Grandchild
    ........... userName3//*Grandchild

    In my case there will be only one *Parent.On run time, based on add new /edit/update functions ,I need to insert a *child or a *Grandchild depending on the data. if a *child data exists I have to get the index of the *child and insert a new *Grandchild else add a new *child data and insert a corresponding *Grandchild .My problem is that
    1.How to get the *child/*Grandchild index based on my data.
    2.I case of a search how to find the *child/*Grandchild and make them selected.

    Any help is appreciated ,thank you.



  • Hi,

    Not sure to understand completely, but you can browse your model recursively with child() method from your root (parent) entry.
    To select an entry, use the setCurrentIndex() method from the view that uses your model.

    Here is a main.cpp file that creates a model, a view, adds some items and selects one of them. It's not exhaustive, but it can give you some hints on how to get information from a model:

    #include <QApplication>
    #include <QStandardItemModel>
    #include <QTreeView>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
    
      auto model = new QStandardItemModel;
      model->appendRow(new QStandardItem("Foo"));
      model->appendRow(new QStandardItem("Bar"));
      model->appendRow(new QStandardItem("Baz"));
    
      QModelIndex indexToSelect;
      for (int k = 0; k < model->rowCount(); ++k) {
        auto currentIndex = model->index(k, 0);
        if (model->itemFromIndex(currentIndex)->data(Qt::DisplayRole) == "Bar") {
          indexToSelect = currentIndex;
          qDebug() << currentIndex;
        }
      }
    
      QTreeView view;
      view.setModel(model);
      view.setCurrentIndex(indexToSelect);
      view.show();
    
      return a.exec();
    }
    
    


  • Hi @ValentinMichelet Thanks for your kind explaination.I am using the treemodel which is sub classed from QAbstractitemmodel as in the example link provided.I have set the model index to the top level and checked for row count so i am able to get the row count but I am unable to explore the child present in each row . Can you please provide a small example to collect all the data present in the tree from top to bottom with the treemodel example so that I can extract my required data.For example in the tree diagram first I will enter into User then check for any child available for User if so then enter into child UserDept1 then check for Grand child if so collect data then move on .. In simlar fashion I want to explore all the items in the treeview from top to bottom.I have tried a lot to get the gist but i couldn't.I hope I am not confusing..Thanks



  • In computer science, there are several ways to explore a tree structure. Here is how I do with Qt model, hopefully this is what you need:

    #include <QApplication>
    #include <QStandardItemModel>
    #include <QTreeView>
    #include <QDebug>
    
    void displayNode(QStandardItemModel* p_model, QModelIndex const& p_index, int p_indent) {
      QString indentString;
      for (int k = 0; k < p_indent; ++k) {
        indentString += " ";
      }
      qDebug() << indentString + p_index.data(Qt::DisplayRole).toString();
    
      for (int k = 0; k < p_model->rowCount(p_index); ++k) {
        displayNode(p_model, p_index.child(k, 0), p_indent+2);
      }
    }
    
    int main(int argc, char *argv[]) {
      QApplication a(argc, argv);
    
      auto model = new QStandardItemModel;
      auto root = new QStandardItem("Root");
      auto foo = new QStandardItem("Foo");
      root->appendRow(foo);
      foo->appendRow(new QStandardItem("Foo1"));
      foo->appendRow(new QStandardItem("Foo2"));
      foo->appendRow(new QStandardItem("Foo3"));
      foo->appendRow(new QStandardItem("Foo4"));
      auto bar = new QStandardItem("Bar");
      root->appendRow(bar);
      bar->appendRow(new QStandardItem("Bar1"));
      bar->appendRow(new QStandardItem("Bar2"));
      auto baz = new QStandardItem("Baz");
      root->appendRow(baz);
      baz->appendRow(new QStandardItem("Baz1"));
      baz->appendRow(new QStandardItem("Baz2"));
      baz->appendRow(new QStandardItem("Baz3"));
      model->appendRow(root);
    
      QModelIndex indexToSelect;
      for (int k = 0; k < model->rowCount(); ++k) {
        auto currentIndex = model->index(k, 0);
        if (model->itemFromIndex(currentIndex)->data(Qt::DisplayRole) == "Bar") {
          indexToSelect = currentIndex;
          qDebug() << currentIndex;
        }
      }
    
      displayNode(model, root->index(), 0);
    
      QTreeView view;
      view.setModel(model);
      view.expandAll();
      view.setCurrentIndex(indexToSelect);
      view.show();
    
      return a.exec();
    }
    

    You can check that your console and the GUI both list the tree in the same order.

    If you want to explore your tree in another order, you will have to study classical tree traversals, and adapt it with QModelIndex API. But the main concept is here: you have a recursive method that calls itself from a node, and you initialize it with your root entity.



  • Hi @ValentinMichelet thank you I almost got it i am working on it...

    Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.