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. [SOLVED] QAbstractItem model and pointer to class as internalPointer.
QtWS25 Last Chance

[SOLVED] QAbstractItem model and pointer to class as internalPointer.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 2.0k Views
  • 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've got Model based on QAbstractItem model. As internatPointers for indaxes i use my class which looks loike this:
    @
    class DeviceItem : public sxProperties {
    QList<DeviceItem *> items; // item's children
    DeviceItem * m_parent;
    }
    @
    where sxProperties class is some QHash based class. but when I look in debug mode some of internal pointers of valid indaxes returns not what I expect but not NULL too. Where is the problem ? Here is some source code of my model:
    @int NetworkTreeModel::rowCount(const QModelIndex & parent) const {

    DeviceItem * parentItem;

    if (parent.column() > 0)
    return 0;

    if (!parent.isValid())
    parentItem = NULL;
    else
    parentItem = static_cast<DeviceItem *> (parent.internalPointer());

    if (parentItem == NULL)
    return m_devices.count();
    else
    return parentItem->childrenCount();
    }@

    @QModelIndex NetworkTreeModel::index(int row, int column, const QModelIndex & parent) const {

    if (!hasIndex(row, column, parent))
    return QModelIndex();

    DeviceItem * parentItem = NULL;

    if (parent.isValid() && parent.internalPointer() != NULL)
    parentItem = static_cast<DeviceItem *> (parent.internalPointer());

    DeviceItem * childItem = NULL;

    if (parentItem)
    childItem = parentItem->child(row);
    else
    childItem = m_devices.at(row);

    if (childItem)
    return createIndex(row, column, childItem);
    else
    return QModelIndex();
    }@

    @QModelIndex NetworkTreeModel::parent(const QModelIndex & child) const {

    if(!child.isValid())
    return QModelIndex();

    if (child.internalPointer() == NULL)
    return QModelIndex();

    DeviceItem * childItem = static_cast<DeviceItem *> (child.internalPointer());

    if (childItem == NULL)
    return QModelIndex();

    DeviceItem * parentItem = childItem->parent();

    if (parentItem == NULL)
    return QModelIndex();

    if (parentItem->parent()) {

    int row = parentItem->row();

    if (row >= 0)
    return createIndex(row, 0, parentItem);

    } else {

    int ndx = m_devices.indexOf(parentItem);

    if(ndx >= 0)
    return createIndex(ndx, 0, parentItem);
    }

    return QModelIndex();
    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      I find the way it works right. I use sortfilterproxy model, and when i call functions of source model with model index taket from view( currentIndex()) the internalPointer() of sourse model contains garbage. When I disable filter model it works fine. Now what can I do to use both models ?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anticross
        wrote on last edited by
        #3

        The probleb is solved. Only thing I need to use mapToSorce for indexes of my filter model to use my source model indexes internal pointers.

        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