Unsolved QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?
-
The
QModelIndex
ref states thatQModelIndex::internalPointer()
returns avoid *
pointer used by the model to associate the index with the internal data structure.. However, apparently the doc section onQStringListModel
says nothing about this data structure. I'm assuming that it must be aQString
but it's better to be safe than sorry.So, does anyone know what's the data structure returned by
QModelIndex::internalPointer()
when using aQStringListModel
? -
Hi,
From a quick test, it returns 0.
-
@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 callQModelIndex::internalPointer()
.Internal details:
- The internal data structure of a
QStringListModel
is... drumroll ...QStringList
! QModelIndex::internalPointer()
is only really needed in a tree model.- List models and table models don't strictly need it because the row and column numbers are enough to uniquely identify the data.
- QStringListModel doesn't use it. It just uses
QModelIndex::row()
: https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qstringlistmodel.cpp.html#_ZNK16QStringListModel4dataERK11QModelIndexi
- If you want to see
internalPointer()
in action, have a look at http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html
- The internal data structure of a
-
@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 -
@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 structureDisagreed. 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()
andQAbstractItemModel::setData()
. - For QStringListModel, the specific data access interface (for the whole set of data) is
QStringListModel::stringList()
andQStringListModel::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 aQVariant
. 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 usingstringList()
/setStringList()
. That covers all scenarios you could possibly need with a QStringListModel, right? - For all Qt models, the generic data access interface is
-
@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. WhenQAbstractItemModel::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 byQModelIndex::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 -
@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()
ormodel->stringList()
don't match the original data.