Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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();
    }@



  • 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 ?



  • 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.


Log in to reply