Qt Labs Forum » ModelTest

qt 4.3.1 demos/interview doesn't pass

(3 posts)

  1. I tried out ModelTest and it helped me find some corner cases that I hadn't
    thought about.

    However, my model fail the childIndex == childIndex1 test and the model->parent(index) != parent test
    (I ended up modify ModelTest so that it only reports problems instead of core dumping.)

    After looking at my code (and the code in "C++ GUI Programming with Qt 4" that I used as a guide)
    I'm not sure these are really problems.

    I put ModelTest into the book's chap10/regexpparser example and that fails the same way. (Actually
    reassuring :-)

    I also put ModelTest into demos/interview at it fails the same way. In fact, it fails over a million times.

    It seems that if you only have one *real* object (eg Node) for all the different columns of a particular
    row there's no way to avoid the model->parent(index) != parent problem.

    I'm fairly new to model/view and it is definitely challenging to rap my head around modelIndex,
    so perhaps I'm missing something.

    Comments?

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

    The interview demo never had the model test run against it. I have fixed the errors it reports and the patch below will be part of 4.3.2.

    When you have one real object for all of the different columns it means that only column 0 has children.

    [parent 0]-[parent 1]-[parent 2]
    - [child 0]-[child 1]-[child 2]

    You want to make sure that hasChildren(), rowCount() and index() reflect that. The reason for this check is that if you call parent on child 0 you want it to always return the exact same value. If index(0, 0 parent 0) returned child 0 and index(0, 0, parent 1) also returned child 0 you can't determine who the parent of child 0 is.

    More often what happens is always returning parent 0 for child 0. So then would get the following
    index(0, 0, parent1).parent() != parent1

    Typically the fix is to improve rowCount and hasChildren to check the column of the index.

    ==== demos/interview/model.cpp#1 - demos/interview/model.cpp ====
    @@ -52,8 +52,7 @@

    int Model::rowCount(const QModelIndex &parent) const
    {
    - Q_UNUSED(parent);
    - return rc;
    + return (parent.isValid() && parent.column() != 0) ? 0 : rc;
    }

    int Model::columnCount(const QModelIndex &parent) const
    @@ -64,6 +63,8 @@

    QVariant Model::data(const QModelIndex &index, int role) const
    {
    + if (!index.isValid())
    + return QVariant();
    if (role == Qt::DisplayRole)
    return "Item " + QString::number(index.row()) + ":" + QString::number(index.column());
    if (role == Qt::DecorationRole) {
    @@ -86,12 +87,15 @@

    bool Model::hasChildren(const QModelIndex &parent) const
    {
    - Q_UNUSED(parent);
    + if (parent.isValid() && parent.column() != 0)
    + return false;
    return rc > 0 && cc > 0;
    }

    -Qt::ItemFlags Model::flags(const QModelIndex &) const
    +Qt::ItemFlags Model::flags(const QModelIndex &index) const
    {
    + if (!index.isValid())
    + return 0;
    return (Qt::ItemIsDragEnabled|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    }

    Posted: 1 year #
  3. Thanks! (!!)

    For me, all I had to do was add

    if (parent.isValid() && parent.column() != 0) {
    return 0;
    }

    to my rowCount function.

    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.