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. QStandardItemModel not inserting children
Forum Updated to NodeBB v4.3 + New Features

QStandardItemModel not inserting children

Scheduled Pinned Locked Moved Solved General and Desktop
qstandarditemmoqmodelindex
8 Posts 3 Posters 3.6k Views 2 Watching
  • 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.
  • TrilecT Offline
    TrilecT Offline
    Trilec
    wrote on last edited by
    #1

    Hi All,
    The following code does not insert the children (or at least not show then), and im not sure i understand why.
    I get the root topnode but no children visible in the QTreeView
    any help appreciated..

    void DialogStructureXML::on_btn_add_shot_released()
    {
        QModelIndex currentIndex    = ui->tree_shot->selectionModel()->currentIndex();
        QModelIndex child      = currentIndex;
        QModelIndex child2           = currentIndex;
        QModelIndex prevIndex;
    
        QStandardItemModel *model = static_cast<QStandardItemModel*>(ui->tree_shot->model());
    
        int rowCount     = model->rowCount(currentIndex);//will also be root if no entries in tree
        int rowCountRoot = model->rowCount();            //will be root as default new QModelIndex()
        int currentRow   = 0;
          if ( !traceIndex.isValid() )
              {
              if (model->columnCount(currentIndex) == 0) {
                  if (!model->insertColumn(0, currentIndex))    return ; //Column index 0 ItemText
                  if (!model->insertColumn(1, currentIndex))    return ; //Column index 1 Description
              }
      
              if (!model->insertRow(currentRow, currentIndex))       return ;
              child = model->index(currentRow, 0, currentIndex); //Column index 0
              model->setData(child, QVariant( "topNode"  ), Qt::EditRole);
      
              child2 = model->index(currentRow, 1, prevIndex); //Column index 1
              model->setData(child2, QVariant( "[Description]"  ), Qt::EditRole);
      
      
              if (!model->insertRow(0, child))       return ;
              child = model->index(0, 0, child); //Column index 0
              model->setData(child, QVariant( "subNode"  ), Qt::EditRole);
      
              child = model->index(0, 1, child); //Column index 1
              model->setData(child, QVariant( "[Description]"  ), Qt::EditRole);
      
              ui->tree_view->selectionModel()->setCurrentIndex(model->index(0, 0, child),QItemSelectionModel::ClearAndSelect);
    }
    }
    

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From a quick look at your code, it's seems that you don't create any item with a parent.

      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
      • TrilecT Offline
        TrilecT Offline
        Trilec
        wrote on last edited by Trilec
        #3

        tks, seems like im not understanding the QModelIndex :

        is:
        QModelIndex currentIndex= ui->tree_shot->selectionModel()->currentIndex();

        not getting a valid parent to attach a row to?
        parent attachment to currentIndex?.

        if (!model->insertRow(currentRow, currentIndex)) return ;

        • this first insert seems to work as I get the first entry. its the next child insertion that seems to fail.

          if (!model->insertRow(currentRow, currentIndex)) return ;
          child= model->index(currentRow, 0, currentIndex); // Column index 0
          model->setData(child, QVariant( "topNode" ), Qt::EditRole);

        • now I use the child to insert, is the the issue?
          I thought the following code was also doing the parent attachment.

        if (!model->insertRow(0, child)) return ;
        child = model->index(0, 0, child); //Column index 0
        model->setData(child, QVariant( "subNode" ), Qt::EditRole);

        would this then be?:
        if (!model->insertRow(0, child.parent() )) return;

        cheers
        c

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What 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
          • TrilecT Offline
            TrilecT Offline
            Trilec
            wrote on last edited by Trilec
            #5

            lol, stamp replies? ....

            if you look at the above code post it says:

            QStandardItemModel *model = static_cast<QStandardItemModel*>(ui->tree_shot->model());
            

            I have been trying a lot of variations and it seem to come down to:

            • Insure you have to a column

            • find the index

            • set the data.

            seems a lot of stuff, to just add data....
            the code i am using...

            index = CreateTopBranch( currentIndex , ui->tree_shot );
            
            
                         if (model->columnCount(index) == 0) {
                             if (!model->insertColumn(0, index))   return;
                            // if (!model->insertColumn(1, index))   return;
                          }
            
                          if (!model->insertRow(0, index))
                              return;
            
                          child = model->index(0, 0, index);
                          model->setData(child, QVariant("[No data]"), Qt::EditRole);
                          
                          child = model->index(0, 1, index);
                          model->setData(child, QVariant("DEscription"), Qt::EditRole);
            
            
            
                          ui->tree_shot->selectionModel()->setCurrentIndex(model->index(0, 0, index),QItemSelectionModel::ClearAndSelect);
            

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

            A 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Ok then, are you using a QTreeWidget or QTreeView ?

              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
              • TrilecT Trilec

                lol, stamp replies? ....

                if you look at the above code post it says:

                QStandardItemModel *model = static_cast<QStandardItemModel*>(ui->tree_shot->model());
                

                I have been trying a lot of variations and it seem to come down to:

                • Insure you have to a column

                • find the index

                • set the data.

                seems a lot of stuff, to just add data....
                the code i am using...

                index = CreateTopBranch( currentIndex , ui->tree_shot );
                
                
                             if (model->columnCount(index) == 0) {
                                 if (!model->insertColumn(0, index))   return;
                                // if (!model->insertColumn(1, index))   return;
                              }
                
                              if (!model->insertRow(0, index))
                                  return;
                
                              child = model->index(0, 0, index);
                              model->setData(child, QVariant("[No data]"), Qt::EditRole);
                              
                              child = model->index(0, 1, index);
                              model->setData(child, QVariant("DEscription"), Qt::EditRole);
                
                
                
                              ui->tree_shot->selectionModel()->setCurrentIndex(model->index(0, 0, index),QItemSelectionModel::ClearAndSelect);
                
                A Offline
                A Offline
                alex_malyu
                wrote on last edited by alex_malyu
                #7

                @Trilec said:

                QStandardItemModel model = static_cast<QStandardItemModel>(ui->tree_shot->model());

                Instance of which class is ui->tree_shot->model()?
                The fact that you cast it to QStandardItemModel does not make it one.
                If it is not QStandardItemModel or derived class you are in undefined behavior area.
                You should not use static_cast for any of QObject derived class anyway. Use qobject_cast and check that value is not nullptr.

                If you are creating model yourself is ui->tree_shot a QTreeWidget?
                If it is custom model you probably did not call proper functions when changing the model.
                In this case widget will not be updated properly.

                1 Reply Last reply
                0
                • TrilecT Offline
                  TrilecT Offline
                  Trilec
                  wrote on last edited by
                  #8

                  Thanks SGaist,
                  will tag as solved.

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

                  1 Reply Last reply
                  0

                  • Login

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