Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模型测试&x2B;简单表模式=父测试失败_C++_Qt_Qt5 - Fatal编程技术网

C++ 模型测试&x2B;简单表模式=父测试失败

C++ 模型测试&x2B;简单表模式=父测试失败,c++,qt,qt5,C++,Qt,Qt5,这里是我的模型的简化版本: class TableModel : public QAbstractTableModel { public: TableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) { } int rowCount(const QModelIndex &parent) const override { return 1; } int columnCount(const QMode

这里是我的模型的简化版本:

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 {}; }
};
如果我以这种方式运行它(使用):

}

它失败于:

// 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
}
我不明白我应该如何修改我的模型来解决这个问题? 看起来像是
QAbstractTableModel::parent中的问题,
换句话说,在Qt代码中,
QAbstractTableModel::parent
是私有的。
QAbstractTableModel
QTableView
建模数据的基础是否错误?

qabstracttemmodel::rowCount
qabstracttemmodel::columnCount
的接口允许视图向模型询问顶级行/列的数量以及特定节点的子节点的数量。前者通过传递父节点
完成,后者通过传递特定节点的QModelIndex作为父节点参数完成

您的
TableModel::rowCount
实现始终返回
1
,即使视图传递了有效的
父节点(即,它请求另一个节点的子节点数)。因为这应该是一个“表”模型(不是树模型),所以您应该按如下方式更改
rowCount
columnCount

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
通过从模型中获取根索引(0,0)的第一个
QModelIndex
,然后询问该子项的错误来检测此类错误。报告的父级应该等于根索引(显然,这在代码中是失败的,因为您没有维护任何这些关系)

childIndex:  QModelIndex(0,0,0x0,QAbstractTableModel(0x7ffd7e2c05a0))
tmp:  QModelIndex(-1,-1,0x0,QObject(0x0))
topIndex:  QModelIndex(0,0,0x0,QAbstractTableModel(0x7ffd7e2c05a0))
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;
    }
    //....
}