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. QAbstractItemModel with QTreeview and accessing through indexing

QAbstractItemModel with QTreeview and accessing through indexing

Scheduled Pinned Locked Moved Solved General and Desktop
qmodelindexqtreeviewqabstractitemmo
9 Posts 2 Posters 9.8k 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.
  • T Offline
    T Offline
    Trilec
    wrote on 11 Mar 2016, 14:16 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Mar 2016, 22:10 last edited by
      #2

      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
      0
      • T Offline
        T Offline
        Trilec
        wrote on 11 Mar 2016, 23:19 last edited by Trilec 3 Nov 2016, 23:25
        #3

        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
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Mar 2016, 23:33 last edited by
          #4

          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

          T 1 Reply Last reply 12 Mar 2016, 07:16
          0
          • S SGaist
            11 Mar 2016, 23:33

            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.

            T Offline
            T Offline
            Trilec
            wrote on 12 Mar 2016, 07:16 last edited by
            #5

            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
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 12 Mar 2016, 21:57 last edited by
              #6

              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
              0
              • T Offline
                T Offline
                Trilec
                wrote on 13 Mar 2016, 11:14 last edited by Trilec
                #7

                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
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 13 Mar 2016, 22:19 last edited by
                  #8

                  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
                  0
                  • T Offline
                    T Offline
                    Trilec
                    wrote on 13 Mar 2016, 23:29 last edited by Trilec
                    #9

                    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
                    0

                    1/9

                    11 Mar 2016, 14:16

                    • Login

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