Results 1 to 4 of 4

Thread: beginInsertRows seems not to be working

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default beginInsertRows seems not to be working

    Hi all,

    I have my own model SettingsTreeModel (which is editable) and here is the part of my code:

    Qt Code:
    1. bool SettingsTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
    2. {
    3.  
    4. StackTrace st("stm setdata");
    5. if (role != Qt::EditRole)
    6. return false;
    7. SettingsNode *node = static_cast<SettingsNode *>(index.internalPointer());
    8. QStringList attributeList;
    9. switch(index.column()) {
    10. case 0:
    11. node->setName(value.toString());
    12. break;
    13. case 1:
    14. node->setAttributeMap(AttributeMap(value.toString()));
    15. break;
    16. case 2:
    17. beginInsertRows(index,0,0); //a new row may be prepended.
    18. st.addExtraInfo(QString("beginInsertRows(%1, %2, %3)").arg(reinterpret_cast<unsigned>(index.internalPointer())).arg(0).arg(0));
    19. node->setValue(value.toString());
    20. endInsertRows(); //maybe?
    21. break;
    22. default:
    23. return false;
    24. }
    25. emit dataChanged(index, index);
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Here the data is changed (new row is inserted) but view doesn't show it. why do u think?

    btw, i tested my model againist qt labs' modeltest and there isn't any error.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: beginInsertRows seems not to be working

    It is hard for me to understand what you are doing :P QAbstractItemModel::setData() is for setting data to existing index. You set your value to your internal structure "node" so I don't see any reason for model to insert any additional row - so there is no reason to call beginInsertRows() or endInsertRows(), which dont insert any rows - just notify view etc about inserting new rows. And I suggest implementing rows inserting in QAbstractItemModel::insertRow() or QAbstractItemModel::insertRows().
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows seems not to be working

    Here is the story about inserting rows in setData:

    this is a kind of tree xml editor, something similar to Simple DOM Model Example (http://doc.trolltech.com/4.2/itemvie...edommodel.html). I already have QAbstractItemModel::insertRow() and QAbstractItemModel::insertRows() implemented, and I called beginInsertRows and it works fine.

    What I did here is this: In DOM specification(http://www.w3.org/TR/REC-DOM-Level-1/), it says that any node which has a text value, will be interpreted like having it's first child element(a text node) with the value of the text.

    So when editing an existing xml file, if there is a node not having any child or value, when user wants to set its value, i should add a child to this node (before any child) and it should be a text node. anyway, i somehow implemented it, and when i edit the value of a node which didn't have any value before, view shows nothing, but when i save file and reload it, i see that actually internally what i wanted has happened and the node has a new child element (#text) and value is set.

    my question is, why i don't see updated tree? any ideas? I can paste here any code on request.

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: beginInsertRows seems not to be working

    the thing is that if you use setData() it is meant that you set data to the existing index. How are editing value node if there is no such node? I don't understand it, because you want to edit not existing index in your model which will make this model to insert this index...
    Then my advice is to insert a new node (index) with insertRow() and then edit its value.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    ugurkoltuk (6th January 2010)

Similar Threads

  1. beginInsertRows was not declared ???
    By travlr in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2009, 21:22
  2. working with SQLite
    By bhs-ittech in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2008, 07:51
  3. Replies: 19
    Last Post: 11th May 2007, 11:23
  4. working with images
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2007, 18:35
  5. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02

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.