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. How to see if a value already exists in the model for treeview?
Forum Updated to NodeBB v4.3 + New Features

How to see if a value already exists in the model for treeview?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.3k 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.
  • Q Offline
    Q Offline
    QtisHard
    wrote on last edited by
    #1

    I have made a model for a treeview.

    I want to see if an item already exists in the model before I append it into the treeview.
    How do I do that?

    3ee24754-639e-4961-b55e-558f0e2cac8d-image.png

    instead of having so many 200s, I want only 1 200 in which all the other values are present.

    Please do guide me

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Look into the model and see if the data you want to add is already there.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        QtisHard
        wrote on last edited by
        #3

        How do I do that?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @QtisHard said in How to see if a value already exists in the model for treeview?:

          How do I do that?

          Since I don't know how you created the model I don't know exactly but iterating through the data of the model shouldn't be that hard.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            QtisHard
            wrote on last edited by
            #5

            https://www.qtcentre.org/threads/44877-Convert-XML-to-QTreeView

            This is what I used to create my model

            Apologies for the continuous questions but I am still in the learning phase so its not that easy for me
            please do guide me to some resources so ik how to proceed

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Since you're using a QStandardItemModel I would suggest to open the documentation for this class and see if you can find a function which looks like you can search for data inside the model. Reading the documentation seems to be really hard.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • Q Offline
                Q Offline
                QtisHard
                wrote on last edited by
                #7

                I did look into this but I was not able to find anything as such

                void Dialog::preOrder(QDomNode dom, QStandardItemModel *model){
                if(dom.isNull())
                {
                return;
                }
                QString aux = dom.nodeName().toStdString().c_str();
                QString qsGroupID = "GroupID";
                QString qsName = "Name";
                if(dom.isText()){
                aux = dom.parentNode().nodeName().toStdString().c_str();
                if(strcmp(aux.toStdString().c_str(),qsGroupID.toStdString().c_str())==0){

                    insertFather(dom.nodeValue());
                    setItemParam(model);
                    }
                    if(strcmp(aux.toStdString().c_str(),qsName.toStdString().c_str())==0){
                       insertChildren(dom.nodeValue());
                      }
                    }else{
                      preOrder(dom.firstChild(), model);
                      preOrder(dom.nextSibling(), model);
                  }
                

                }

                This is the code I have to insert the items into the model view. What should i change so that all the values dont come and they come as a common parent

                JonBJ Christian EhrlicherC 2 Replies Last reply
                0
                • Q QtisHard

                  I have made a model for a treeview.

                  I want to see if an item already exists in the model before I append it into the treeview.
                  How do I do that?

                  3ee24754-639e-4961-b55e-558f0e2cac8d-image.png

                  instead of having so many 200s, I want only 1 200 in which all the other values are present.

                  Please do guide me

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @QtisHard said in How to see if a value already exists in the model for treeview?:

                  I want to see if an item already exists in the model before I append it into the treeview.

                  I did look into this but I was not able to find anything as such

                  What about QList<QStandardItem *> QStandardItemModel::findItems(const QString &text, Qt::MatchFlags flags = Qt::MatchExactly, int column = 0) const ?

                  1 Reply Last reply
                  1
                  • Q QtisHard

                    I did look into this but I was not able to find anything as such

                    void Dialog::preOrder(QDomNode dom, QStandardItemModel *model){
                    if(dom.isNull())
                    {
                    return;
                    }
                    QString aux = dom.nodeName().toStdString().c_str();
                    QString qsGroupID = "GroupID";
                    QString qsName = "Name";
                    if(dom.isText()){
                    aux = dom.parentNode().nodeName().toStdString().c_str();
                    if(strcmp(aux.toStdString().c_str(),qsGroupID.toStdString().c_str())==0){

                        insertFather(dom.nodeValue());
                        setItemParam(model);
                        }
                        if(strcmp(aux.toStdString().c_str(),qsName.toStdString().c_str())==0){
                           insertChildren(dom.nodeValue());
                          }
                        }else{
                          preOrder(dom.firstChild(), model);
                          preOrder(dom.nextSibling(), model);
                      }
                    

                    }

                    This is the code I have to insert the items into the model view. What should i change so that all the values dont come and they come as a common parent

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @QtisHard
                    On a completely separate note, what about getting rid of all these unnecessary and ugly .toStdString().c_str()? And we don't use strcmp() any longer these days.

                    QString aux = dom.nodeName().toStdString().c_str();
                    

                    dom.nodeName() returns a QString. You then convert that to a std::string, get the C string out of that and assign it to QString aux, which requires converting a C string all the way back to a QString! QString aux = dom.nodeName() is all you need.

                    if (strcmp(aux.toStdString().c_str(),qsGroupID.toStdString().c_str())==0)
                    

                    Why not

                    if (aux == qsGroupID)
                    

                    ?! Isn't that shorter, clearer and easier (plus more efficient)? :)

                    1 Reply Last reply
                    1
                    • Q QtisHard

                      I did look into this but I was not able to find anything as such

                      void Dialog::preOrder(QDomNode dom, QStandardItemModel *model){
                      if(dom.isNull())
                      {
                      return;
                      }
                      QString aux = dom.nodeName().toStdString().c_str();
                      QString qsGroupID = "GroupID";
                      QString qsName = "Name";
                      if(dom.isText()){
                      aux = dom.parentNode().nodeName().toStdString().c_str();
                      if(strcmp(aux.toStdString().c_str(),qsGroupID.toStdString().c_str())==0){

                          insertFather(dom.nodeValue());
                          setItemParam(model);
                          }
                          if(strcmp(aux.toStdString().c_str(),qsName.toStdString().c_str())==0){
                             insertChildren(dom.nodeValue());
                            }
                          }else{
                            preOrder(dom.firstChild(), model);
                            preOrder(dom.nextSibling(), model);
                        }
                      

                      }

                      This is the code I have to insert the items into the model view. What should i change so that all the values dont come and they come as a common parent

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @QtisHard said in How to see if a value already exists in the model for treeview?:

                      I did look into this but I was not able to find anything as such

                      Then you should look again... searching for 'find' would be a good start.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1

                      • Login

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