Results 1 to 9 of 9

Thread: Problem with TreeView

  1. #1
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with TreeView

    Hello all,

    I've been trying for hours to get this QTreeView / QAbstractItemModel code snippet to work with Qt4.4 and VC++ 2008 Express. When I expand the nested items twice, Qt fails with the assert "ASSERT: "i > -1" in file itemviews\qtreeview.cpp, line 3013". Am I doing something wrong?

    Qt Code:
    1. #include <QtGui>
    2. class TestModel;
    3.  
    4. class SceneTest : public QDialog {
    5. public:
    6. SceneTest();
    7. protected:
    8. QTreeView *tree;
    9. TestModel *model;
    10. };
    11.  
    12. class InternalItem {
    13. public:
    14. InternalItem() {
    15. parent = NULL;
    16. }
    17. ~InternalItem() {qDeleteAll(children);}
    18. int row() {
    19. if (parent) {
    20. return parent->children.indexOf(this);
    21. } else {
    22. return 0;
    23. }
    24. }
    25. InternalItem *append(InternalItem *iitem) {children.append(iitem); return (iitem->parent = this);}
    26. InternalItem *parent;
    27. QList < InternalItem * > children;
    28. };
    29.  
    30. class TestModel : public QAbstractItemModel {
    31. public:
    32. TestModel();
    33. QVariant data(const QModelIndex &index, int role) const;
    34. Qt::ItemFlags flags(const QModelIndex &index) const;
    35. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    36. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    37. int columnCount(const QModelIndex &parent) const;
    38. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    39. QModelIndex parent(const QModelIndex &) const;
    40. protected:
    41. InternalItem *root;
    42. };
    43.  
    44. TestModel::TestModel() {
    45. root = new InternalItem();
    46. root->append(new InternalItem());
    47. root->append(new InternalItem());
    48. root->children[1]->append(new InternalItem());
    49. root->children[1]->children[0]->append(new InternalItem());
    50. root->children[1]->children[0]->children[0]->append(new InternalItem());
    51. }
    52.  
    53. Qt::ItemFlags TestModel::flags(const QModelIndex &index) const {
    54. return index.isValid() ? (Qt::ItemIsEnabled | Qt::ItemIsSelectable) : 0;
    55. }
    56.  
    57. QVariant TestModel::data(const QModelIndex &index, int role) const {
    58. if (!index.isValid() || index.column() >= 1)
    59. return QVariant();
    60. if (role == Qt::DisplayRole) {
    61. InternalItem *iitem = static_cast < InternalItem * >(index.internalPointer());
    62. if (index.column() == 0) {
    63. return (long)iitem;
    64. }
    65. }
    66. return QVariant();
    67. }
    68.  
    69. QModelIndex TestModel::index(int row, int column, const QModelIndex &parent) const {
    70. if (!hasIndex(row, column, parent)) {
    71. return QModelIndex();
    72. }
    73. InternalItem *parentItem = NULL;
    74. if (!parent.isValid()) {
    75. parentItem = root;
    76. } else {
    77. parentItem = static_cast<InternalItem*>(parent.internalPointer());
    78. }
    79. InternalItem *child = parentItem->children[row];
    80. if (child) {
    81. return createIndex(row, column, child);
    82. } else {
    83. return QModelIndex();
    84. }
    85. }
    86.  
    87. QModelIndex TestModel::parent(const QModelIndex &index) const {
    88. if (!index.isValid()) return QModelIndex();
    89. InternalItem *iindex = static_cast < InternalItem * >(index.internalPointer());
    90. if (!iindex->parent || iindex->parent == root) {
    91. return QModelIndex();
    92. } else {
    93. return createIndex(iindex->row(), 0, iindex->parent);
    94. }
    95. }
    96.  
    97. QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const {
    98. if (section == 0 && role == Qt::DisplayRole) {
    99. return QString("A");
    100. } else {
    101. return QVariant();
    102. }
    103. }
    104.  
    105. int TestModel::rowCount(const QModelIndex &parent) const {
    106. if (parent.column() > 0) return 0;
    107. if (parent.isValid()) {
    108. return static_cast < InternalItem * >(parent.internalPointer())->children.size();
    109. } else {
    110. return root->children.size();
    111. }
    112. }
    113.  
    114. int TestModel::columnCount(const QModelIndex &parent) const {
    115. return 1;
    116. }
    117.  
    118. SceneTest::SceneTest() : QDialog(NULL) {
    119. model = new TestModel();
    120. tree = new QTreeView(this);
    121. tree->setModel(model);
    122. QBoxLayout *layout = new QHBoxLayout(this);
    123. layout->addWidget(tree);
    124. }
    To copy to clipboard, switch view to plain text mode 
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with TreeView


  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with TreeView

    Did you forget to setup Q_OBJECT macro in your classes?
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with TreeView

    Markosan, I guess it's better to use Q_OBJECT on the model, but since I wasn't using signals yet, it wasn't really necessary. In any case, I tried it and received the same result.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  5. #5
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with TreeView

    Hello Patrik, I'm not entirely sure how that post applies, since I really want to use the current design. I'm not really interested in editing either. My design is based on Qt's simpletreemodel example, so I know that I'm not too far away from working code.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with TreeView

    I dont say to swap design the file
    http://ppk.ciz.ch/win_build/tree_cat_memo/Tree_Cat.zip
    show only how to build a recursive function on model tree on XML
    and other model to make the same on SQL or faster on sqlite3

    if you have a recursive build model
    you dont need this here ...

    Qt Code:
    1. root->children[1]->append(new InternalItem());
    2. root->children[1]->children[0]->append(new InternalItem());
    3. root->children[1]->children[0]->children[0]->append(new InternalItem());
    To copy to clipboard, switch view to plain text mode 

    sql tree query

    #define MINISQL_TREE_NO_LIMIT "SELECT n.*, round((n.rgt-n.lft-1)/2,0) AS childs, count(*)+(n.lft>1) AS level, ((min(p.rgt)-n.rgt-(n.lft>1))/2) > 0 AS lower, (( (n.lft-max(p.lft)>1) )) AS upper FROM catememo n, catememo p WHERE n.lft BETWEEN p.lft AND p.rgt AND (p.root_id = n.root_id) AND (p.id != n.id OR n.lft = 1) GROUP BY n.root_id,n.id ORDER BY n.root_id,n.lft"

    structure

    query.exec("create table catememo (id INTEGER PRIMARY KEY AUTOINCREMENT,root_id INTEGER,name varchar(110),lft INTEGER,rgt INTEGER,oldid INTEGER,xmlattribute BLOB)");

    Or online demo:
    http://www.klempert.de/nested_sets/demo/

  7. #7
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with TreeView

    I really don't think XML or databases would be useful, since I'm just trying to show the hierarchy of a QGraphicsScene for a fancy scene debugger I'm working on. The code you quoted was just there for debugging purposes, since it shows the bug. The model I wrote (not the test case in this post) can successfully show the hierarchy right now, except it crashes just like this example when it is expanded twice.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem with TreeView

    The problem seems to be in TestModel::parent():
    Qt Code:
    1. return createIndex(iindex->row(), 0, iindex->parent);
    2. // vs.
    3. return createIndex(iindex->parent->row(), 0, iindex->parent);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    init2null (25th May 2008)

  10. #9
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    5
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with TreeView

    That's it. Thank you! If and when my QGraphicsScene visualization tool becomes useful, I'll be sure to post it here.
    ImageRocket - My Qt4-based image editing program
    Project Page / Screenshots / Source

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.