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

QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?



  • The QModelIndex ref states that QModelIndex::internalPointer() returns a void * pointer used by the model to associate the index with the internal data structure.. However, apparently the doc section on QStringListModel says nothing about this data structure. I'm assuming that it must be a QString but it's better to be safe than sorry.

    So, does anyone know what's the data structure returned by QModelIndex::internalPointer() when using a QStringListModel ?


  • Lifetime Qt Champion

    Hi,

    From a quick test, it returns 0.


  • Moderators

    @rmam You should only call QModelIndex::internalPointer() when you are implementing your own custom model. Since you are using an existing model (QStringListModel), don't call QModelIndex::internalPointer().

    Internal details:



  • @JKSH in that case the QModelIndex docs sorely need an updated as they don't mention any of your observations, particularly your comment on how they aren't used in general.

    Nevertheless, if the role of the model in a MVC architecture is to manage data, including access to the underlying data structure, then access to the internal data structure is no corner case.

    Moreover, it doesn't make much sense to rely on QStringListModel:.data() to access the model's internal data structure as it returns representations of the underlying data that's specific to a certain visualization need, which may or may not be the original data.



  • @rmam said in QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?:

    if the role of the model in a MVC architecture is to manage data, including access to the underlying data structure

    exactly. that's the role of the model, not of code that uses that model.

    As @JKSH said:

    QModelIndex::internalPointer() is only really needed in a tree model.

    In case of QStringListModel, QModelIndex::row() completely defines the mapping with the internal data so all other parameters are set to 0


  • Moderators

    @rmam said in QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?:

    @JKSH in that case the QModelIndex docs sorely need an updated as they don't mention any of your observations, particularly your comment on how they aren't used in general.

    I agree, there is much room for improvement in the model/view docs.

    Nevertheless, if the role of the model in a MVC architecture is to manage data...

    Agreed, the model manages the data.

    Note: Qt uses a simplified form of the MVC -- there is no Controller (see https://doc.qt.io/qt-5/model-view-programming.html )

    ...including access to the underlying data structure, then access to the internal data structure is no corner case.

    Moreover, it doesn't make much sense to rely on QStringListModel:.data() to access the model's internal data structure

    Disagreed. If you can access the internal data directly, that means you can bypass the model's data management logic.

    Here's a quick thought: Suppose you modify the internal data directly without using the model's methods. How can the model know? Furthermore, how can the model notify the view to update the display, when it doesn't even realize that that the data has changed?

    The model class encapsulates the internal data. By the fundamental principles of object-oriented programming, the model class should forbid direct access to the internal data. Instead, we access the data via the interface(s) provided by the class:

    • For all Qt models, the generic data access interface is QAbstractItemModel::data() and QAbstractItemModel::setData().
    • For QStringListModel, the specific data access interface (for the whole set of data) is QStringListModel::stringList() and QStringListModel::setStringList().

    it returns representations of the underlying data that's specific to a certain visualization need, which may or may not be the original data.

    What do you mean? QAbstractItemModel::data() returns your data (a string, in this case) wrapped inside a QVariant. You can convert the variant to a string (and vice-versa)

    QStringListModel *model = ...
    
    // Read the 5th list element
    QString myStr = model->data( model->index(4) ).toString();
    
    // Write the 10th list element
    model->setData( model->index(9), "Hello World" );
    

    This is analogous with how we'd use a raw QStringList:

    QStringList list = ...
    
    // Read the 5th list element
    QString myStr = list[4];
    
    // Write the 10th list element
    list[9] = "Hello World";
    

    To summarize, QStringListModel lets you access individual items using data()/setData(), and lets you access the entire data structure using stringList()/setStringList(). That covers all scenarios you could possibly need with a QStringListModel, right?



  • @JKSH said in QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?:

    Disagreed. If you can access the internal data directly, that means you can bypass the model's data management logic.

    That's the problem: you're not accessing the internal data directly. The QAbstractItemModel::data() member function is used to provide representations of the original data intended to be used in specific visualization roles. When QAbstractItemModel::data() is called, the member function returns a value that's specific to the data role, which may or may not match the original data. What does return the original data, or at least is expected to serve that purpose, is the pointer to the internal data structure that should be provided by QModelIndex::internalPointer().



  • @rmam said in QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?:

    What does return the original data, or at least is expected to serve that purpose

    No, it does not. I think you misunderstood the entire role of the internal pointer.
    The internal pointer is used to provide a mapping, not represent the data. In all the cases I can think of, the internal pointer is used to determine what item is the parent of the current one in a tree, nothing more


  • Moderators

    @rmam said in QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?:

    When QAbstractItemModel::data() is called, the member function returns a value that's specific to the data role, which may or may not match the original data. What does return the original data, or at least is expected to serve that purpose, is...

    Please explain how model->data( model->index(i) ).toString() or model->stringList() don't match the original data.


Log in to reply