Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modeltest + simple table mode = parent test failed

Tags:

c++

qt

qt5

Here simplified version of my model:

class TableModel : public QAbstractTableModel {
public:
  TableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {
  }
  int rowCount(const QModelIndex &parent) const override { return 1; }
  int columnCount(const QModelIndex &parent) const override { return 2; }
  QVariant data(const QModelIndex &idx, int role) const override { return {}; }
};

If I run it in such way (with Qt model test):

int main(int argc, char *argv[]) {
  QApplication app(argc, argv);

  TableModel tbl_model;
  ModelTest mtest{&tbl_model, nullptr};

}

it failed at:

// Common error test #1, make sure that a top level index has a parent
// that is a invalid QModelIndex.
QModelIndex topIndex = model->index(0, 0, QModelIndex());
tmp = model->parent(topIndex);
Q_ASSERT(tmp == QModelIndex());

// Common error test #2, make sure that a second level index has a parent
// that is the first level index.
if (model->rowCount(topIndex) > 0) {
    QModelIndex childIndex = model->index(0, 0, topIndex);
    qDebug() << "childIndex: " << childIndex;
    tmp = model->parent(childIndex);
    qDebug() << "tmp: " << tmp;
    qDebug() << "topIndex: " << topIndex;
    Q_ASSERT(tmp == topIndex);//assert failed
}

and print:

childIndex:  QModelIndex(0,0,0x0,QAbstractTableModel(0x7ffd7e2c05a0))
tmp:  QModelIndex(-1,-1,0x0,QObject(0x0))
topIndex:  QModelIndex(0,0,0x0,QAbstractTableModel(0x7ffd7e2c05a0))

I can not understand how should I modify my model to fix this issue? Looks like the problem in QAbstractTableModel::parent, in other words in Qt code, and QAbstractTableModel::parent is private. Is QAbstractTableModel wrong base for modeling of data for QTableView?

like image 274
user1244932 Avatar asked Dec 02 '25 10:12

user1244932


1 Answers

QAbstractItemModel::rowCount and QAbstractItemModel::columnCount's interface allows the view to ask the model for the number of top-level rows/columns as well as asking for the number of children a specific node has. The former is done by passing in an invalid parent, while the latter is done by passing the specific node's QModelIndex as the parent parameter.

Your TableModel::rowCount's implementation always returns 1 even when the view passes a valid parent (i.e. It is asking for the number of the children of another node). Since this is supposed to be a "Table" model (not a tree model), You should change your rowCount and columnCount as follows:

class TableModel : public QAbstractTableModel {
    // .....
    int rowCount(const QModelIndex &parent) const override {
        if(parent.isValid()) return 0; //no children
        return 1;
    }
    int columnCount(const QModelIndex &parent) const override {
        if(parent.isValid()) return 0; //no children
        return 2;
    }
    //....
}

ModelTest detects such mistakes by getting the first child QModelIndex for the root index (0,0) from your model and then asking this child about its parent. The reported parent should equal the root index (obviously, this fails in your code since you are not maintaining any of these relationships)...

like image 56
Mike Avatar answered Dec 04 '25 22:12

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!