Qt Forum

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

    Solved QAbstractItemModel with QTreeview and accessing through indexing

    General and Desktop
    qmodelindex qtreeview qabstractitemmo
    2
    9
    8629
    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.
    • Trilec
      Trilec last edited by

      Hi All Super brains.

      A question regarding QAbstractItemModel used in a QTreeview and its access from a button slot.

      I'm experimenting with the Example "/widgets/itemviews/editabletreemodel".

      My question is how do you Add only to the root a child, even if the selectionModel()->currentIndex() is pointing to a different branch (or child )

      void Dialog::on_btn_add_3branch_released()
      {
          QModelIndex index = ui->tree_test->selectionModel()->currentIndex();
          QAbstractItemModel *model =  ui->tree_test->model();
      ...
      //Add only to current branch
      // if already 3nd branch add  to current branch as sibling node.
      //if 0=index (root?) Add parents till you reach 3nd branch then add  sibling node.
      }
      

      In this use case The user can only ADD to 3 children deep, ( there are 3 buttons representing each depth. 1, 2 and 3)
      If the user selects a button for creating a child at the 3rd depth, it will create the needed branches till it reaches the correct depth then add the child.

      one attempt:

       //Create Scene node off root
              QModelIndex indexRoot = model->index(0, 0, QModelIndex());
              if (!model->insertRow(0, indexRoot ))   return; //try to create at root
              model->setData(indexRoot , QVariant( "[rootChild]"  ), Qt::EditRole);
      ...
      

      If the user "is" on the last 3rd depth as reported by "selectionModel()->currentIndex() " and the user presses to add depth 1 (or 0) the should be able to backup to the root child add add to that.

      its all related to the parent child relationship by I cant seem to quite master the detail

      any help appreciated.

      If ts not in the computer it doesn't exist!...

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        If you want to go back to the "root" index of a branch, you can follow back the trail using the QModelIndex::parent. When parent is invalid, you reached the top level item.

        Hope it helps

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

          Thanks,
          So no easy way to get Root node without parent back trailing?,
          was hoping :
          QModelIndex indexRoot = model->index(0, 0, QModelIndex()); would give the root.

          throught im not sure why this is not in the QTreeview ..ie ui->tree_test->selectionModel()->rootIndex();

          or method of returning what tree depth you are in the tree from QTreeview ?

          If ts not in the computer it doesn't exist!...

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Something's not clear, do you want the current root node of the model or the top level node of a branch ?

            For the current root index: myTreeView->rootIndex();

            You can change the root index used by the view to accommodate your needs. The model doesn't have that notion because its job is to manage data not what the view considers to be the root index.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Trilec 1 Reply Last reply Reply Quote 0
            • Trilec
              Trilec @SGaist last edited by

              thanks, both types interest me.
              I can get the model data from :
              QAbstractItemModel *model = ui->tree_test->model();

              The top level node of a branch is what im interesting in finding.
              (as seen from the QTreeView).

              If ts not in the computer it doesn't exist!...

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                What kind of model are you using ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                  The model is closley based on the the Example "/widgets/itemviews/editabletreemodel".
                  After testing this following code seems to work ( in adding to the root) by passing a blank QModelIndex().
                  ie: if (!model->insertRow(0, QModelIndex())) return;

                  only issue im still trying to resolve now is the backtrailing.
                  If the TreeView currentIndex selection is several rows down (root children) AND several branches deep, I want to insert at the top level root another child (but after the currentIndex selections rootchild ). but finding all these index's required for navigating is a bit confusing.

                  void Dialog::on_btn_add_topbranch_released()
                  {
                      QModelIndex index = ui->tree_view->selectionModel()->currentIndex();
                      tree_model *model =  ui->tree_view->model();
                     QModelIndex indexRoot; ui->tree_shot->rootIndex(); //if set to rootNode
                  
                      QModelIndex child;
                  
                      if (model->columnCount(index) == 0) {
                          if (!model->insertColumn(0, index))
                              return;
                      }
                  
                  //chech if the index is valid from the view ,if not then its at the rootnode anyway
                      if ( !index.isValid() )
                      {
                          if (!model->insertRow(0, index))      
                                     return;
                          child = model->index(0, 0, index); //Column index 0
                          model->setData(child, QVariant( "[Top at root]"  ), Qt::EditRole);
                          return;
                  }
                  
                  //otherwise force adding off root
                      if (!model->insertRow(0, QModelIndex()))       return;
                     // or could also use :
                   //if (!model->insertRow(0, indexRoot))       return;
                      child = model->index(0, 0, QModelIndex()); //Column index 0
                      model->setData(child, QVariant( "[Top at root]"  ), Qt::EditRole);
                  }
                  // but where to realy insert with knowledge of the selections top root child?
                  

                  If ts not in the computer it doesn't exist!...

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Each index that is in a branch has a parent so basically you have to loop until the parent is null then you have the start index of your branch.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                      Cool, Thanks for all the insite: I have solved the problem with a little more testing.
                      For the sake of completeness and for others Ive included my code:

                      void DialogStructureXML::on_btn_add_toroot_released()
                      {
                          QModelIndex currentIndex    = ui->tree->selectionModel()->currentIndex();
                          QModelIndex rootIndex       = ui->tree->rootIndex();
                          QModelIndex traceIndex      = currentIndex;
                          QModelIndex child           = currentIndex;
                      
                          tree_model *model = (tree_model*) ui->tree->model();
                          int rowCount = model->rowCount();
                          int currentRow = 0;
                          int prevRow = 0;
                      
                          //some string cleanup
                          QString ItemText = ui->edit_textvaluefortree->text() ;
                          ItemText.simplified();
                      
                          //If its a valid index (ie: valid something in the GUI tree) trace back to the parent
                          if ( traceIndex.isValid() ) {  
                                  while (  traceIndex.isValid() ) {
                                      currentRow = traceIndex.row()+1;
                                      traceIndex = model->parent(traceIndex);
                                  }
                          }
                          
                          if (!model->insertRow(currentRow, rootIndex))       return;
                          child = model->index(currentRow, 0, rootIndex); //Column index 0
                          model->setData(child, QVariant( ItemText  ), Qt::EditRole);
                          return;
                      }
                      

                      If ts not in the computer it doesn't exist!...

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