Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QTreeView set index and make selected

    General and Desktop
    2
    5
    6650
    Loading More Posts
    • 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.
    • S
      Shiv last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • ValentinMichelet
        ValentinMichelet last edited by ValentinMichelet

        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();
        }
        
        
        1 Reply Last reply Reply Quote 1
        • S
          Shiv last edited by

          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

          ValentinMichelet 1 Reply Last reply Reply Quote 0
          • ValentinMichelet
            ValentinMichelet @Shiv last edited by ValentinMichelet

            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.

            1 Reply Last reply Reply Quote 0
            • S
              Shiv last edited by

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

              Thank you

              1 Reply Last reply Reply Quote 0
              • First post
                Last post