Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    4
    9
    1002
    Loading More Posts
    • 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.
    • R
      rmam last edited by

      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 ?

      JKSH 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        From a quick test, it returns 0.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • JKSH
          JKSH Moderators @rmam last edited by

          @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:

          • The internal data structure of a QStringListModel is... drumroll ...QStringList!
            • https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qstringlistmodel.h.html#QStringListModel::lst
          • 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

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          R 1 Reply Last reply Reply Quote 4
          • R
            rmam @JKSH last edited by

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

            VRonin JKSH 2 Replies Last reply Reply Quote 0
            • VRonin
              VRonin @rmam last edited by VRonin

              @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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply Reply Quote 2
              • JKSH
                JKSH Moderators @rmam last edited by JKSH

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

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                R 1 Reply Last reply Reply Quote 3
                • R
                  rmam @JKSH last edited by rmam

                  @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().

                  VRonin JKSH 2 Replies Last reply Reply Quote 0
                  • VRonin
                    VRonin @rmam last edited by

                    @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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply Reply Quote 1
                    • JKSH
                      JKSH Moderators @rmam last edited by

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

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post