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



  • 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



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



  • 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 ;)



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



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


  • Moderators

    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



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


  • Moderators

    [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. :-)



  • 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).



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.