Results 1 to 8 of 8

Thread: Creating children in QtreeWidget with SQL data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Post Creating children in QtreeWidget with SQL data

    Hello
    I have two tables in SQL database and 1st table I have created as a parent In QtreeWidgit using QtreeWidgetItems now I have to add children in parents items respectly, here is the example

    table1

    Name
    1. Marcus
    2. Andreas
    3. Jacop



    table 2

    Column 1
    Devices
    1. Laptop
    2. Cell Phon
    3. Pc
    4. Laptop
    5. PC
    6. Cell Phon


    Column 2
    Name
    1. Marcus
    2. Marcus
    3. Marcus
    4. Andreas
    5. Andreas
    6. Jacop




    Here table 1 is my parent items and table 2 has two column first is my children items and second I have given for the reference.
    Now I have added parent in my QtreeWidget using QtreeWidgetItem but I am not able to add children saperatly.

    Main problem I am getting is, Wheneever I add children, All children were added In each parent .

    Looking for your advise
    Thanks in advance.
    Last edited by sam123; 16th June 2016 at 11:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Creating children in QtreeWidget with SQL data

    Looks like you forgot to post the code you are using for adding the child items.

    Cheers,
    _

  3. #3
    Join Date
    May 2016
    Posts
    17
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: Creating children in QtreeWidget with SQL data

    Here is the code,

    Qt Code:
    1. QList<QTreeWidgetItem*>item_list;
    2.  
    3. QSqlQuery query;
    4. query.prepare("select Name from table1 ");
    5. query.exec();
    6. while(query.next())
    7. {
    8. QTreeWidgetItem * tree_item = new QTreeWidgetItem();
    9. tree_item->setText(0,query.value(0).toString());
    10. item_list.append(tree_item);
    11. ui->treeWidget->addTopLevelItems(item_list); // Adding Tablel 1 as a parents
    12.  
    13. query.prepare("select Device from table2");
    14. query.exce();
    15. while(query.next())
    16. {
    17. QTreeWidgetItem * child_item = new QTreeWidgetItem();
    18. child_item->setText(0,query.value(0).toString());
    19. item_list.append(child_item);
    20. tree_item->addTopLevelItems(item_list); // Adding Childen accourding to parents referance(not working)
    21. // every chid being adding in every parents.
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    My Output:
    1. Marcus
      1. Laptop
      2. Cell Phon
      3. PC
      4. Laptop
      5. PC
      6. Cell Phone
    2. Andreas
      1. Laptop
      2. Cell Phon
      3. PC
      4. Laptop
      5. PC
      6. Cell Phone
    3. Jacop
      1. Laptop
      2. Cell Phon
      3. PC
      4. Laptop
      5. PC
      6. Cell Phone


    Expected Output:
    1. Marcus
      1. Laptop
      2. Cell Phon
      3. PC
    2. Andreas
      1. Laptop
      2. PC
    3. Jacop
      1. Cell Phone

    Last edited by sam123; 16th June 2016 at 11:22.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Creating children in QtreeWidget with SQL data

    item_list is defined out side of the loop, so the same instance is used in each loop iteration.
    You are adding the same list without ever clearing it.

    Just don't use that list, you don't need it.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 24th February 2016, 17:00
  2. Replies: 4
    Last Post: 22nd January 2013, 19:53
  3. QTreeWidget disable children
    By hlvietlong in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2009, 01:34
  4. creating different types of mdi children
    By OriginalCopy in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2008, 16:51
  5. Replies: 1
    Last Post: 19th October 2007, 01:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.