Results 1 to 8 of 8

Thread: Creating children in QtreeWidget with SQL data

  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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    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,
    _

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

    Default Re: Creating children in QtreeWidget with SQL data

    thanks

    without list how would I add list of data??


    Added after 16 minutes:


    will you please tell what changes I have to do for gbetting expected output?
    Last edited by sam123; 16th June 2016 at 12:18.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating children in QtreeWidget with SQL data

    1. For both queries You are using this same QSqlQuery object.
    2. In line 13 You should select only equipment belonging to person not all.

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

    Default Re: Creating children in QtreeWidget with SQL data

    Thank you so much

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

    Default Re: Creating children in QtreeWidget with SQL data

    Quote Originally Posted by sam123 View Post
    without list how would I add list of data??
    You don't add a list, you only add the item you just created.
    QTreeWidgetItem::addChild() or pass the parent item to the child item's constructor.

    The latter also works for top level items if you pass the tree widget to the item's constructor.

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.