Results 1 to 2 of 2

Thread: Adding new items to model (MVC)

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Adding new items to model (MVC)

    Hello!
    I've created my own model, that bases on Blanchet-Summerfield methon from all-known book.
    So, I have an Node object. Every Node has a pointer to parent node and a list of child node. Model has the "main Root Node", which parent == 0 ad child are the needed model's nodes.
    The "main Root Node" doesn't appear in view, only its nodes:
    Qt Code:
    1. RootNode - invisiple
    2. --Node1
    3. ----Node1.1
    4. ----Node1.2
    5. --Node2
    6. --Node3
    To copy to clipboard, switch view to plain text mode 

    At first time I fill this model by data using beginResetModel and endResetModel functions.
    But there are cases, when I want to add a few nodes into the model, but don't want to reset it whole. Additionaly, I use a proxy sorting model.

    Reading Qt help I see that I should use beginInsertRows and endInsertRows functions. The problem is that functions have a QModelIndex parameter - parent node.
    I don't use QModelIndex at all!
    To know node class from QModelIndex presented I use this construction, as written in the book:
    Qt Code:
    1. tUMClientsNode *lNode = static_cast<tUMClientsNode*>(aParent.internalPointer());
    To copy to clipboard, switch view to plain text mode 
    tUMClientsNode - my node class, aParent - QModelIndex.

    What should I do to get QModelIndex from my Node? How to use beginInsertRows correctly?

    To add a single item I use similar code now:
    Qt Code:
    1. void tUMClientsModel::AddNewClient(tUMDCClientInfo &aNewClient, bool aUseBeginInsert)
    2. {
    3. if (aNewClient.fClientParentID == 0)
    4. fRootNode->fChildrenList.append(new tUMClientsNode(fRootNode, aNewClient));
    5. else
    6. for (int i = 0; i < fRootNode->fChildrenList.count(); i++)
    7. if (fRootNode->fChildrenList[i]->fClientInfo.fClientID == aNewClient.fClientParentID)
    8. {
    9. fRootNode->fChildrenList[i]->fChildrenList.append(
    10. new tUMClientsNode(fRootNode->fChildrenList[i], aNewClient)
    11. );
    12. break;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  2. #2
    Join Date
    Feb 2010
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding new items to model (MVC)

    Here's full code of my model: http://rapidshare.com/files/42935747...odel_urvin.zip
    Please!

Similar Threads

  1. QListView - adding items
    By creep33 in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 17:30
  2. adding one icon to all items in QTreeView
    By Mystical Groovy in forum Qt Programming
    Replies: 6
    Last Post: 6th October 2009, 12:13
  3. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03
  4. Limitation when adding more than 256 items to a model
    By darkadept in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2006, 15:26
  5. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04

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.