Results 1 to 2 of 2

Thread: getting data out of the simple tree model example

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default getting data out of the simple tree model example

    Fooling around with the treemodel, I hoped this would work. I want to wrte the contents of the treemodel in the simpletreemodel example to a text file. I thought the index function would get the correct data from the child items if the column counter > 0, so that the contents of the children are simply accessible with model.data(i,j). All I get is a list of contents of the parent items (so at index i,0) , I cannot acces the content of the children.
    Qt Code:
    1. QFile fout("some.txt");
    2. fout.open(QIODevice::ReadWrite);
    3. for (int i = 0; i < model.rowCount(); i++)
    4. {
    5. QString S = "";
    6. for (int j = 0; j < model.columnCount(); j++)
    7. {
    8. QVariant d = model.data(model.index(i,j),0);
    9. S = S + d.toString()+";";
    10. }
    11. S = S + "\n";
    12. QByteArray line(S.toAscii());
    13. fout.write(line);
    14. }
    15. fout.close();
    To copy to clipboard, switch view to plain text mode 
    my reasoning is not correct obviously, but I don't get it. Thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: getting data out of the simple tree model example

    ok, I am thinking about this as a table, every parent has a different nr children, and each child has several strings in my case (a data input structure for some software I am making). This works better :
    Qt Code:
    1. QFile fout("hup.txt");
    2. fout.open(QIODevice::ReadWrite);
    3. for (int i = 0; i < model.rowCount(); i++)
    4. {
    5. QVariant d = model.data(model.index(i,0),0); // parent
    6. QString S = "["+QString::number(i)+"-" + QString::number(0) + "] " + d.toString()+";\n";
    7.  
    8. QModelIndex indexParent = model.index(i, 0);
    9. for (int j = 0; j < model.rowCount(indexParent); j++)
    10. {
    11. S = S + "["+QString::number(i)+"-" + QString::number(j) + "] ";
    12.  
    13. for (int k = 0; k < model.columnCount(indexParent); k++)
    14. {
    15. d = model.data(model.index(j, k, indexParent),0);
    16. S = S + d.toString()+";";
    17. }
    18. S = S + "\n";
    19. }
    20. QByteArray line(S.toAscii());
    21. fout.write(line);
    22. }
    23. fout.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by qt_gotcha; 7th July 2009 at 08:04.

Similar Threads

  1. A simple example of a tree model
    By YaK in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2009, 17:15
  2. Writing a Tree model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2009, 11:22
  3. Creating a Tree Model from a QStandardModel
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 06:47
  4. Help needed in creating Tree Model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2008, 12:14
  5. using the example of simple tree model
    By krishna.bv in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2006, 12:28

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.