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. Model/View : Is it necessary to check the model index supplied is valid ?
Qt 6.11 is out! See what's new in the release blog

Model/View : Is it necessary to check the model index supplied is valid ?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 6.6k Views 1 Watching
  • 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
    redstoneleo
    wrote on last edited by
    #1

    In this example , Is it necessary to check the model index supplied is valid and the row number is within the range of items in the string list ?
    http://qt-project.org/doc/qt-4.8/model-view-programming.html#model-headers-and-data

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Of course it is necessary, unless you provide a decent mechanism to ensure the correctness of the data.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goblincoding
        wrote on last edited by
        #3

        I am a big supporter of "defensive programming" and tend to assume that I'm going to get something I don't like and cater for it.

        Rather safe than sorry ;)

        http://www.goblincoding.com

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          If you say pass a random index from the already existing indices then it is safe, but other than similar safe situations, I'd rather do the check, it doesn't take much.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

            If you really need to optimize out the check for performance reasons (what other reason would there be?), I suggest that you at least leave that check there inside an assert() statement. Then the check can remain enabled for Debug builds but it will be left out easily in the final Release build...

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              I think the question stems from this:
              @
              if (!index.isValid())
              return QVariant();

               if (index.row() >= stringList.size())
                   return QVariant();
              

              @

              and is more about understanding. It looks a bit strange that the index is checked for validity and proven to be valid, but apparently the validity check is not sufficient.

              Without knowing the details of the example, I guess isValid is more a general check of the index properties. The second check indicates the index points really to a valid entry. You need to derive your own class and reimplement isValid to cover this as well.

              All the other answers cover already and goblincoding summarized it neatly: Rather safe than sorry

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                According to the docs:
                "A valid index belongs to a model, and has non-negative row and column numbers."

                So in a "valid" QModelIndex, the row/column index may still exceed the number rows/columns you actually have in your model and thus it needs to be checked separately.

                More specifically, a default-constructed QModelIndex() always is invalid by definition! You normally obtain a "valid" index via QAbstractItemModel::createIndex(). In a way, you can think of the default-constructed QModelIndex() as a "NULL" index and the isValid() check is used to check whether you have such a NULL-index.

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  [quote author="MuldeR" date="1366468540"]According to the docs:
                  "A valid index belongs to a model, and has non-negative row and column numbers."

                  So in a "valid" QModelIndex, the row/column index may still exceed the number rows/columns you actually have in your model and thus it needs to be checked separately.

                  More specifically, a default-constructed QModelIndex() always is invalid by definition! You normally obtain a "valid" index via QAbstractItemModel::createIndex(). In a way, you can think of the default-constructed QModelIndex() as a "NULL" index and the isValid() check is used to check whether you have such a NULL-index.[/quote]

                  You are completely right.

                  If you see this example code isolated, it looks strange. :-)

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Achab
                    wrote on last edited by
                    #9

                    In general, for "valid" it does not just mean that it is not a Null Index. If you are creating your own model (not using one from Qt), it is possible to use your own data structure. The data structure do not need to use QModelIndex (i.e. an array), but the model uses QModelIndex, you should check that a QModelIndex has a valid data (i.e not out of array bounds).

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      redstoneleo
                      wrote on last edited by
                      #10

                      I decided to be safe rather than sorry——have both of them in my code whenever possible.

                      1 Reply Last reply
                      0

                      • Login

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