Results 1 to 3 of 3

Thread: QStandardItemModel's insertRow() did't make a grandchild item in QTreeView

  1. #1
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QStandardItemModel's insertRow() did't make a grandchild item in QTreeView

    When I intended to write a small code using QTreeView and QStandardItemModel.
    I encountered a problem.

    When I made a grandchild item, using QStandardItemModel::insertRow(int row, const QModelIndex & parent = QModelIndex()),
    the item did not appeared in the tree view.

    In the following code, Case1 worked good.
    But, contrary to my expectation, Case2 did not work.

    Is it correct to make a grandchild item, using QStandardItemModel::insertRow() ?

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtCore/QModelIndex>
    3. #include <QtGui/QStandardItem>
    4. #include <QtGui/QStandardItemModel>
    5. #include <QTreeView>
    6. #include <QtDebug>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication application(argc, argv);
    11.  
    12. QTreeView treeView;
    13. treeView.setModel(&model);
    14.  
    15. // (Case 1)
    16. // QStandardItem* rootItem = model.invisibleRootItem();
    17. // rootItem->insertRow(0, new QStandardItem("Child Item"));
    18. // QStandardItem* childItem = rootItem->child(0, 0);
    19. // if (childItem)
    20. // childItem->insertRow(0, new QStandardItem("Grandchild Item"));
    21.  
    22. // (Case 2)
    23. model.insertRow(0, new QStandardItem("Child Item"));
    24. QModelIndex childItemIndex = model.index(0, 0, QModelIndex());
    25. bool isSuccess = false;
    26. if (childItemIndex.isValid()) {
    27. isSuccess = model.insertRow(0, childItemIndex); // make a grandchild, but the item does not appear
    28. }
    29. qDebug() << "isSuccess : " << isSuccess; // true
    30.  
    31. childItemIndex = model.index(0, 0, QModelIndex());
    32. qDebug() << "childItemIndex.isValid : " << childItemIndex.isValid(); // true
    33. QModelIndex grandchildItemIndex = model.index(0, 0, childItemIndex);
    34. qDebug() << "grandchildItemIndex.isValid() : " << grandchildItemIndex.isValid(); // false
    35.  
    36. treeView.show();
    37.  
    38. return application.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    I'm sorry in poor English.
    kichi

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStandardItemModel's insertRow() did't make a grandchild item in QTreeView

    You need to insert a column as well. The insertRow which takes a QStandardItem does this automatically.
    Qt Code:
    1. if (childItemIndex.isValid()) {
    2. isSuccess = model.insertRow(0, childItemIndex); // make a grandchild, but the item does not appear
    3. isSuccess = model.insertColumn(0, childItemIndex); // Need this...
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to numbat for this useful post:

    kichi (17th January 2010)

  4. #3
    Join Date
    Nov 2007
    Posts
    31
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel's insertRow() did't make a grandchild item in QTreeView

    Quote Originally Posted by numbat View Post
    You need to insert a column as well. The insertRow which takes a QStandardItem does this automatically.
    Qt Code:
    1. if (childItemIndex.isValid()) {
    2. isSuccess = model.insertRow(0, childItemIndex); // make a grandchild, but the item does not appear
    3. isSuccess = model.insertColumn(0, childItemIndex); // Need this...
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your advice has solved the problem.
    Thank you very much.
    kichi

Similar Threads

  1. QTreeView::item stylesheet
    By Baasie in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 14:55
  2. QTreeView and item editing
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 18:56
  3. QStandardItemModel insertRow crashing
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 27th June 2008, 11:55
  4. QTableView QTablemodel & insertRow
    By pfusterschmied in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 12:43
  5. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24

Tags for this Thread

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.