Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QAbstractItemView asks model for invalid index

I'm writting a program using Qt5 which works fine on Linux but on Windows I was observing strange behaviour:

When QTreeView::setModel was called, it asked model for index (QAbstractItemModel::index) with some row and col and invalid parent. It never happend on Linux, view always asked for hasChildren, rowCount etc before calling index.

I've downloaded Qt5's sources to see what's going on and I can see:

// These asserts do basic sanity checking of the model
Q_ASSERT_X(d->model->index(0,0) == d->model->index(0,0),
           "QAbstractItemView::setModel",
           "A model should return the exact same index "
           "(including its internal id/pointer) when asked for it twice in a row.");
Q_ASSERT_X(!d->model->index(0,0).parent().isValid(),
           "QAbstractItemView::setModel",
           "The parent of a top level index should be invalid");

I cannot find a single word about those sanity checks in documentation of view classes nor model classes.

Where are they defined?

Another interesting thing here is that I could deduce by observation of model/view classes I've written that top index should be invalid but I couldn't find this information directly in docs.

like image 784
Michał Walenciak Avatar asked Aug 15 '14 13:08

Michał Walenciak


People also ask

What is a qabstractitemview class?

QAbstractItemView class is the base class for every standard view that uses a QAbstractItemModel. QAbstractItemView is an abstract class and cannot itself be instantiated. It provides a standard interface for interoperating with models through the signals and slots mechanism, enabling subclasses to be kept up-to-date with changes to their models.

Is it possible to re-implement the qabstractitemview API?

The QAbstractItemView API is large, and at the time of this writing, the Qt documentation does not explicitly specify which parts of the API must be reimplemented by subclasses and which base class implementations are sufficient. However, some of the methods are pure virtual and so must be reimplemented.

What is qabstractitemdelegate in PySide?

PySide2.QtWidgets.QAbstractItemDelegate Returns the item delegate used by this view and model for the given index . PySide2.QtWidgets.QAbstractItemDelegate Returns the item delegate used by this view and model for the given column .

How do I force editing of a qabstractitemview?

The action that caused the editing process is described by trigger, and the associated event is specified by event. Editing can be forced by specifying the trigger to be QAbstractItemView::AllEditTriggers. See also closeEditor (). Reimplements: QAbstractScrollArea::event (QEvent *event).


1 Answers

From the documentation of QAbstractItemModel::parent():

If the item has no parent, an invalid QModelIndex is returned.

This means that calling index() with an invalid QModelIndex asks for top level items.

The sanity checks you encountered may have been disabled in Linux (maybe release build?) - but your model's functionality should in no way depend on the order of function calls.

If index() is called with invalid row / column parameters (also if your model is not populated yet), return QModelIndex().

like image 132
Martin Hennings Avatar answered Nov 15 '22 09:11

Martin Hennings