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.
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.
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.
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 .
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).
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With