How to see if a value already exists in the model for treeview?
-
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?instead of having so many 200s, I want only 1 200 in which all the other values are present.
Please do guide me
-
Look into the model and see if the data you want to add is already there.
-
@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.
-
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 -
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.
-
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
-
@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
-
@QtisHard
On a completely separate note, what about getting rid of all these unnecessary and ugly.toStdString().c_str()
? And we don't usestrcmp()
any longer these days.QString aux = dom.nodeName().toStdString().c_str();
dom.nodeName()
returns aQString
. You then convert that to astd::string
, get the C string out of that and assign it toQString aux
, which requires converting a C string all the way back to aQString
!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)? :)
-
@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.