Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractItemModel beginInsertItem and update model problem.

QAbstractItemModel beginInsertItem and update model problem.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    I'm writing class which inherits QAbstractItemModel and has a 2-level tree structure , this model is using other class which has it's own structure. I have such a problem when my structure changes. I know about all changes by signals which emitted my structure class. So i insert row first in a structure class then i emit signal and need to update my model. When I use reset model it's work perfect, but i need to update only that place where i hawe some changes. So here what I wrote:
    @void SessionsTreeModel::onSessionCreated( int sessionID ) {
    int index = GET_SESSION_MANAGER()->getSessionList().findIndexById(sessionID);

    if (index == -1) {
        return;
    }
    
    if (m_sessionIDs.contains(sessionID)) {
        return;
    }
    
    m_sessionIDs.insert(sessionID,index);
    
    beginInsertRows(QModelIndex(),index,index);
    endInsertRows();
    
    SharedSessionItem sessionItem = GET_SESSION_MANAGER()->getSessionList().getSessionByID(sessionID);
    
    if (sessionItem.isNull()) {
        return;
    }
    
    QList<sxProperties> targets = sessionItem->getTargets();
    
    for (int i = 0; i < targets.count(); i++) {
        onTargetAppended(sessionID, targets.at(i).valueS(PropName::name));
    }
    

    }
    //---------------------------------------------------------------------------------------------------------------
    void SessionsTreeModel::onSessionRemoved( int sessionID ) {
    int index = m_sessionIDs.take(sessionID);
    beginRemoveRows(QModelIndex(),index,index);
    endRemoveRows();
    }
    //---------------------------------------------------------------------------------------------------------------
    void SessionsTreeModel::onTargetAppended( int sessionID, const QString & target ) {
    int parentIndex = m_sessionIDs.value(sessionID, -1);//GET_SESSION_MANAGER()->getSessionList().findIndexById(sessionID);

    if (parentIndex == -1) {
        return;
    }
    
    SharedSessionItem sessionItem = GET_SESSION_MANAGER()->getSessionList().getSessionByID(sessionID);
    
    if (sessionItem.isNull()) {
        return;
    }
    
    int index = sessionItem->getTargetIndex(target);
    
    if (index == -1) {
        return;
    }
    
    QModelIndex parent = this->index(parentIndex,eName,QModelIndex());
    
    if (parent.isValid() == false) {
        return;
    }
    
    QHash<QString,int> targets = m_targetNames.take(sessionID);
    
    if (targets.contains(target)) {
        m_targetNames.insert(sessionID,targets);
        return;
    }
    
    targets.insert(target,index);
    m_targetNames.insert(sessionID,targets);
    
    beginInsertRows(parent,index,index);
    endInsertRows();
    

    }
    //---------------------------------------------------------------------------------------------------------------
    void SessionsTreeModel::onTargetRemoved( int sessionID, const QString & target ) {
    int parentIndex = m_sessionIDs.value(sessionID, -1);

    if (parentIndex == -1) {
        return;
    }
    
    
    QHash<QString,int> targets = m_targetNames.take(sessionID);
    
    if (targets.contains(target) == false) {
        m_targetNames.insert(sessionID,targets);
        return;
    }
    
    int index = targets.take(target);
    m_targetNames.insert(sessionID,targets);
    
    QModelIndex parent = this->index(parentIndex,eName,QModelIndex());
    
    if (parent.isValid() == false) {
        return;
    }
    
    beginRemoveRows(parent,index,index);
    endRemoveRows();
    

    }
    //---------------------------------------------------------------------------------------------------------------@

    Where @private: QHash<int,int> m_sessionIDs; // holds row by given id for remove session
    private: QHash<int,QHash<QString, int>> m_targetNames; // first key = sessionID
    //included hash holds row by given name for remove target
    private: enum eColumns {eName,eUrl,eSource, eState, eDate,eTime,eMax}; // eName - id of session,
    //or Target name who receives session@

    and @GET_SESSION_MANAGER()@ gives instance to my structure class.
    When I start my model I only have one item 1-st level item. When i add 1-st level items it updates, but when add 2-nd level items it's updates only when i collaps\expand it's root. So please help.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hmm, the endRemoveRows() does emit a signal to update the view. I would suggest looking into the View that you use if there is a "expand" level setting.
      The model looks correct to me.

      Greetz, Jeroen

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved