How to iterate through a QStandardItemModel completely?










4














I have a QStandardItemModel, which I display in q QTreeView. Works fine.



To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.



QStringList namesToBeHighlighted = getNames();

QModelIndex in = myModel->index(0, 0);

if ( in.isValid() )

for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)

QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);

if (curItem)
QString curItemName = curItem->text();

if ( namesToBeHighlighted.contains(curItem->text()) )
curItem->setFont(highlightFont);

else curItem->setFont(unHighlightFont);





My Model has following structure:

Level_1

+--> Level_11

+--> Level_12

+--> Level_13

Level_2

+--> Level_21

+--> Level_22

+--> Level_23

...



Here, it iterates trough Levels 11, 12 and 13 then it stops.










share|improve this question




























    4














    I have a QStandardItemModel, which I display in q QTreeView. Works fine.



    To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.



    QStringList namesToBeHighlighted = getNames();

    QModelIndex in = myModel->index(0, 0);

    if ( in.isValid() )

    for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)

    QModelIndex si = myModel->index(curIndex, 0, in);
    QStandardItem *curItem = myModel->itemFromIndex(si);

    if (curItem)
    QString curItemName = curItem->text();

    if ( namesToBeHighlighted.contains(curItem->text()) )
    curItem->setFont(highlightFont);

    else curItem->setFont(unHighlightFont);





    My Model has following structure:

    Level_1

    +--> Level_11

    +--> Level_12

    +--> Level_13

    Level_2

    +--> Level_21

    +--> Level_22

    +--> Level_23

    ...



    Here, it iterates trough Levels 11, 12 and 13 then it stops.










    share|improve this question


























      4












      4








      4







      I have a QStandardItemModel, which I display in q QTreeView. Works fine.



      To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.



      QStringList namesToBeHighlighted = getNames();

      QModelIndex in = myModel->index(0, 0);

      if ( in.isValid() )

      for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)

      QModelIndex si = myModel->index(curIndex, 0, in);
      QStandardItem *curItem = myModel->itemFromIndex(si);

      if (curItem)
      QString curItemName = curItem->text();

      if ( namesToBeHighlighted.contains(curItem->text()) )
      curItem->setFont(highlightFont);

      else curItem->setFont(unHighlightFont);





      My Model has following structure:

      Level_1

      +--> Level_11

      +--> Level_12

      +--> Level_13

      Level_2

      +--> Level_21

      +--> Level_22

      +--> Level_23

      ...



      Here, it iterates trough Levels 11, 12 and 13 then it stops.










      share|improve this question















      I have a QStandardItemModel, which I display in q QTreeView. Works fine.



      To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.



      QStringList namesToBeHighlighted = getNames();

      QModelIndex in = myModel->index(0, 0);

      if ( in.isValid() )

      for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)

      QModelIndex si = myModel->index(curIndex, 0, in);
      QStandardItem *curItem = myModel->itemFromIndex(si);

      if (curItem)
      QString curItemName = curItem->text();

      if ( namesToBeHighlighted.contains(curItem->text()) )
      curItem->setFont(highlightFont);

      else curItem->setFont(unHighlightFont);





      My Model has following structure:

      Level_1

      +--> Level_11

      +--> Level_12

      +--> Level_13

      Level_2

      +--> Level_21

      +--> Level_22

      +--> Level_23

      ...



      Here, it iterates trough Levels 11, 12 and 13 then it stops.







      c++ qt5 qstandarditemmodel qmodelindex qstandarditem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 5:40









      Cœur

      17.5k9104145




      17.5k9104145










      asked Oct 14 '15 at 12:08









      Ralf WickumRalf Wickum

      40211547




      40211547






















          1 Answer
          1






          active

          oldest

          votes


















          7














          I hope it helps you:



          void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex()) 
          for(int r = 0; r < model->rowCount(parent); ++r)
          QModelIndex index = model->index(r, 0, parent);
          QVariant name = model->data(index);
          qDebug() << name;
          // here is your applicable code
          if( model->hasChildren(index) )
          forEach(model, index);




          QStandardItemModel model;
          QStandardItem* parentItem = model.invisibleRootItem();
          for (int i = 0; i < 4; ++i)
          QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
          for (int j = 0; j < 5; ++j)
          item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));

          parentItem->appendRow(item);
          parentItem = item;

          forEach(&model);





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33124903%2fhow-to-iterate-through-a-qstandarditemmodel-completely%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            I hope it helps you:



            void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex()) 
            for(int r = 0; r < model->rowCount(parent); ++r)
            QModelIndex index = model->index(r, 0, parent);
            QVariant name = model->data(index);
            qDebug() << name;
            // here is your applicable code
            if( model->hasChildren(index) )
            forEach(model, index);




            QStandardItemModel model;
            QStandardItem* parentItem = model.invisibleRootItem();
            for (int i = 0; i < 4; ++i)
            QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
            for (int j = 0; j < 5; ++j)
            item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));

            parentItem->appendRow(item);
            parentItem = item;

            forEach(&model);





            share|improve this answer

























              7














              I hope it helps you:



              void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex()) 
              for(int r = 0; r < model->rowCount(parent); ++r)
              QModelIndex index = model->index(r, 0, parent);
              QVariant name = model->data(index);
              qDebug() << name;
              // here is your applicable code
              if( model->hasChildren(index) )
              forEach(model, index);




              QStandardItemModel model;
              QStandardItem* parentItem = model.invisibleRootItem();
              for (int i = 0; i < 4; ++i)
              QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
              for (int j = 0; j < 5; ++j)
              item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));

              parentItem->appendRow(item);
              parentItem = item;

              forEach(&model);





              share|improve this answer























                7












                7








                7






                I hope it helps you:



                void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex()) 
                for(int r = 0; r < model->rowCount(parent); ++r)
                QModelIndex index = model->index(r, 0, parent);
                QVariant name = model->data(index);
                qDebug() << name;
                // here is your applicable code
                if( model->hasChildren(index) )
                forEach(model, index);




                QStandardItemModel model;
                QStandardItem* parentItem = model.invisibleRootItem();
                for (int i = 0; i < 4; ++i)
                QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
                for (int j = 0; j < 5; ++j)
                item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));

                parentItem->appendRow(item);
                parentItem = item;

                forEach(&model);





                share|improve this answer












                I hope it helps you:



                void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex()) 
                for(int r = 0; r < model->rowCount(parent); ++r)
                QModelIndex index = model->index(r, 0, parent);
                QVariant name = model->data(index);
                qDebug() << name;
                // here is your applicable code
                if( model->hasChildren(index) )
                forEach(model, index);




                QStandardItemModel model;
                QStandardItem* parentItem = model.invisibleRootItem();
                for (int i = 0; i < 4; ++i)
                QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
                for (int j = 0; j < 5; ++j)
                item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));

                parentItem->appendRow(item);
                parentItem = item;

                forEach(&model);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 14 '15 at 13:28









                AnatolySAnatolyS

                3,6081023




                3,6081023



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33124903%2fhow-to-iterate-through-a-qstandarditemmodel-completely%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Darth Vader #20

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Ondo