Qt Labs Forum » ModelTest

modeltest "problems"

(2 posts)
  • Started 1 year ago by tommyd
  • Latest reply from Benjamin Meyer

Tags:


  1. Hi!

    I just added the modeltest code to my project and indeed it finds problems in my code. Now, I was pretty sure I had followed the HOWTO in the docs very closely (f.e. [0]) when I created my first model a few months back, so I now wonder how relevant certain assertions are in real life, i.e. will they ever make problems if certain reimplemented methods like data(), index() aso. are called from other parts of the Interview architecture?

    One certain assertion is the test if negative column/row numbers are correctly handled in QWhateverModel::index(). The relevant code in [0] for this is

    QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
                 const
     {
         TreeItem *parentItem;
    
         if (!parent.isValid())
             parentItem = rootItem;
         else
             parentItem = static_cast<TreeItem*>(parent.internalPointer());
    
         TreeItem *childItem = parentItem->child(row);
         if (childItem)
             return createIndex(row, column, childItem);
         else
             return QModelIndex();
     }

    Now since there are no checks for row < 0 or column < 0 I wonder why createIndex returns an invalid index here on purpose? Or maybe, one should fix up this example =)

    Thomas.

    [0] http://doc.trolltech.com/4.2/itemviews-simpletreemodel.html

    Posted: 1 year #
  2. Benjamin Meyer
    Benjamin Meyer
    Administrator

    Thanks for the suggestion, I have updated the simpletreemodel and simpledommodel example so starting in 4.3 code based upon it will pass the test.

    The test calls the functions with out of bounds functions like index(-1,-1, QModelIndex()) which 'should' never happen in the real world and in fact there are internal tests to catch if any of the views do that. The test was originally written for the Qt models to make sure that they could handle code I have seen such as:

    void MyClass::myFunction(int row)
    {
    dirmodel->index(row, 0, QModelIndex());
    }

    Here the application could accidently pass in a negative number to the QDirModel.

    Posted: 1 year #

RSS feed for this topic

Reply

You must log in to post.



© 2008 Nokia Corporation and/or its subsidiaries. Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.
All other trademarks are property of their respective owners.