Important: Please read the Qt Code of Conduct -

QStandardItemModel sibling always returns valid index [Qt 5.0]

  • @
    #include <QCoreApplication>
    #include <QStandardItemModel>
    #include <qDebug>

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    QStandardItemModel *model = new QStandardItemModel();

    model->appendRow(new QStandardItem("1"));
    model->appendRow(new QStandardItem("2"));

    QModelIndex idx = model->index(0,0);
    qDebug() << ((idx.isValid()) ? : "Invalid index");

    idx = idx.sibling(1,0);
    qDebug() << ((idx.isValid()) ? : "Invalid index");

    idx = idx.sibling(666,0); //evil -- this is out of range
    qDebug() << ((idx.isValid()) ? : "Invalid index");

    return a.exec();
    QVariant(QString, "1")
    QVariant(QString, "2")

    Should I consider this as a bug?

  • This is not a BUG.
    If you see QAbstractItemModel::data which is called by QStandardItemModel::data...
    The doc clearly states that, an invalid QVariant is returned instead of returning 0.
    Therefore the o/p of your program is:
    QVariant(QString, "1")
    QVariant(QString, "2")

  • QModelIndex QAbstractItemModel::sibling(int row, int column, const QModelIndex & index) const [virtual]

    Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no sibling at that location.

    so the case is not about the QVariant which is invalid from obvious reasons but why the index returned by sibling is valid

Log in to reply