Results 1 to 4 of 4

Thread: QAbstractItemModel Subclass duplicate entries

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QAbstractItemModel Subclass duplicate entries

    Hello people,

    I've successfully subclassed QAbstractItemModel to fit my needs. It acts as a frontend for the ls command. Ls feeds output in the following form to my model:
    ./parentfolder:
    subfolder1/
    subfolder2/

    ./parentfolder/subfolder1:
    dir/
    dir2/
    textfile.txt

    ./parentfolder/subfolder1/dir:

    ./parentfolder/subfolder1/dir2:

    ./parentfolder/subfolder2:
    textfile1.txt
    textfile2.txt
    textfile3.txt
    And displays it like in the attached image.
    The only thing I can't get fixed is the multiple occurence of parents. I've been struggeling with it the whole day so I hope someone with a fresh mind can see what's wrong with my code:

    Qt Code:
    1. void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
    2. {
    3. QList<TreeItem*> parents;
    4. QList<int> indentations;
    5. parents << parent;
    6. indentations << 0;
    7.  
    8. for( int r=0; r<lines.count(); r++ ) {
    9. QString lineData = lines[r].trimmed();
    10.  
    11. if (!lineData.isEmpty()) {
    12. qDebug() << QString(lineData) << "\n";
    13.  
    14. // Line is a dir
    15. if (lineData.endsWith(":")) {
    16. // Get rid of the colon
    17. lineData.chop(1);
    18.  
    19. // Check if the string is a dot from the beginning of the ls output
    20. if (lineData == ".") {
    21. // Get rid of the string
    22. lineData.clear();
    23. }
    24.  
    25. // Check if the line starts with a dot and get rid of it before we replace the backslashes
    26. if (lineData.startsWith("./")) {
    27. lineData.replace(QString("./"), QString("/"));
    28. }
    29. qDebug() << lineData;
    30.  
    31. QStringList list = lineData.split("/", QString::SkipEmptyParts);
    32. qDebug() << list << "start appending parent/child shit \n ";
    33.  
    34. // Reset the parent FIXME: horrible method
    35. parents << parents.first();
    36. for(int p = 0; p < list.size(); p++) {
    37.  
    38. QList<QVariant> column;
    39. column << list[p];
    40.  
    41. parents.last()->appendChild(new TreeItem(column, parents.last()));
    42. parents << parents.last()->child(parents.last()->childCount()-1);
    43. }
    44.  
    45. } else if (lineData.endsWith("/")) {
    46. qDebug() << "is a directory conjo!!!" << lineData << "\n";
    47. } else {
    48. QList<QVariant> column;
    49. column << lineData;
    50.  
    51. parents.last()->appendChild(new TreeItem(column, parents.last()));
    52. qDebug() << "appended file: " << lineData << "\n";
    53. }
    54. }
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    So, in short: I need my QAbstractItemModel to treat items with the same name under the same parent as one item, and not as duplicates.

    Thanks in advance,
    pvdk
    Attached Images Attached Images

Similar Threads

  1. QAbstractItemModel subclass for a treeview
    By pvdk in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 00:13
  2. Replies: 1
    Last Post: 2nd February 2007, 08:42

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.