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. QStringListModel: what's the data structure pointed to by QModelIndex::internalPointer() ?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.2k 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.
  • R Offline
    R Offline
    rmam
    wrote on last edited by
    #1

    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 ?

    JKSHJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • R rmam

        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 ?

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @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
        4
        • JKSHJ JKSH

          @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
          R Offline
          R Offline
          rmam
          wrote on last edited by
          #4

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

          VRoninV JKSHJ 2 Replies Last reply
          0
          • R rmam

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @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
            2
            • R rmam

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

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @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
              3
              • JKSHJ 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?

                R Offline
                R Offline
                rmam
                wrote on last edited by rmam
                #7

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

                VRoninV JKSHJ 2 Replies Last reply
                0
                • R 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().

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

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

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

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

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved