Delete an item from a qtreewidget linked to a sqlite database
-
wrote on 20 Aug 2021, 14:48 last edited by
Hello,
I'm coming to you to ask you about a problem with a QTreeWidget. My tree is linked to a SQL database. I want that when I delete a parent, all its children are deleted in the tree but also in the database.
Here is an example of my tree :
I tried a code but the problem is that item with id "27" is not deleted in my database. Item with id "25,26,28" are well deleted. And i don't understand why...
Here is my code:void modeadmin::deleterow(QTreeWidgetItem *parent) { connOpen(); //function which open the connection with my database QString id; QSqlQuery qry; id=parent->text(); qry.prepare("Delete from Element where id='"+id+"'"); // sql query to delete all the id matching with the children id qry.exec(); for(int i=0;i<parent->childCount();i++) { deleterow(parent->child(i)); } delete parent; }
If someone can help me, thanks a lot !
-
wrote on 20 Aug 2021, 15:19 last edited by JoeCFD
@coilo said in Delete an item from a qtreewidget linked to a sqlite database:
for(int i=0;i<parent->childCount();i++) <<=====the child count changes after any child is deleted. { deleterow(parent->child(i)); }
try this
int count = parent->childCount();
for(int i=count-1;i>=0;--i)
{
deleterow(parent->child(i));
} -
wrote on 20 Aug 2021, 14:56 last edited by
-
wrote on 20 Aug 2021, 15:19 last edited by JoeCFD
@coilo said in Delete an item from a qtreewidget linked to a sqlite database:
for(int i=0;i<parent->childCount();i++) <<=====the child count changes after any child is deleted. { deleterow(parent->child(i)); }
try this
int count = parent->childCount();
for(int i=count-1;i>=0;--i)
{
deleterow(parent->child(i));
} -
wrote on 20 Aug 2021, 16:05 last edited by
Yes that was it ! It works well thanks a lot !
1/4