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. Delete an item from a qtreewidget linked to a sqlite database

Delete an item from a qtreewidget linked to a sqlite database

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 290 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.
  • C Offline
    C Offline
    coilo
    wrote on 20 Aug 2021, 14:48 last edited by
    #1

    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 :
    2a00ca65-aa3c-411b-baac-3c369de91cec-image.png

    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 !

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 20 Aug 2021, 15:19 last edited by JoeCFD
      #3

      @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));
      }

      1 Reply Last reply
      1
      • C Offline
        C Offline
        coilo
        wrote on 20 Aug 2021, 14:56 last edited by
        #2

        I tried with a bigger tree :
        3f2f8ccc-27d7-45a8-8b1c-1191a9e952fb-image.png

        But only id "25,26,28,30" are deleted...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JoeCFD
          wrote on 20 Aug 2021, 15:19 last edited by JoeCFD
          #3

          @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));
          }

          1 Reply Last reply
          1
          • C Offline
            C Offline
            coilo
            wrote on 20 Aug 2021, 16:05 last edited by
            #4

            Yes that was it ! It works well thanks a lot !

            1 Reply Last reply
            0

            1/4

            20 Aug 2021, 14:48

            • Login

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