绝不会调用QAbstractItemModel data()

问题描述 投票:7回答:3

我正在尝试创建一个QTreeView并使用自定义模型。我已经在各个地方放置了qDebug()声明,我已经确定data()从未被调用过。我该如何解决这个问题?

该模型的代码如下

#include "ModelItemNeural.h"

ModelItemNeural::ModelItemNeural(QObject *parent, NeuralNode *rootNode)
    : QAbstractItemModel(parent)
{
    this->rootNode = 0;
}

QModelIndex ModelItemNeural::index(int row, int column, const QModelIndex &parent) const
{
    // Out of bounds and null rootNode check.
    if (rootNode == 0 || row < 0 || column < 0)
    {
        return QModelIndex();
    }

    NeuralNode* parentNode = nodeFromIndex(parent);
    NeuralNode* childNode = parentNode->getInputs().value(row);

    if (childNode == 0)
    {
        return QModelIndex();
    }

    return createIndex(row, column, childNode);
}

QModelIndex ModelItemNeural::parent(const QModelIndex &child) const
{
    NeuralNode* node = nodeFromIndex(child);
    if (node == 0)
    {
        return QModelIndex();
    }

    NeuralNode* parentNode = node->getParent();
    if (parentNode == 0)
    {
        return QModelIndex();
    }

    NeuralNode* grandParentNode = parentNode->getParent();
    if (grandParentNode == 0)
    {
        return QModelIndex();
    }

    int row = grandParentNode->getInputs().indexOf(parentNode);
    return createIndex(row, 0, parentNode);
}

int ModelItemNeural::rowCount(const QModelIndex& parent) const
{
    if (parent.isValid() == false)
    {
        return 0;
    }

    if (parent.column() > 0)
    {
        return 0;
    }

    NeuralNode* parentNode = nodeFromIndex(parent);
    if (parentNode == 0)
    {
        return 0;
    }

    return parentNode->getInputs().length();
}

int ModelItemNeural::columnCount(const QModelIndex &parent) const
{
    return 2;
}

QVariant ModelItemNeural::data(const QModelIndex &index, int role) const
{
    qDebug() << "Data";
    if (index.isValid() == false)
    {
        return QVariant();
    }

    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    NeuralNode* node = nodeFromIndex(index);
    if (node == 0)
    {
        return QVariant();
    }

    switch (index.column())
    {
        case 0:
        {
            // Stripping the name of the NeuralNode type.
            QString name = typeid(node).name();
            int index = name.indexOf(" ");
            if (index >= 0)
            {
                name = name.remove(0, index + 1);
            }

            qDebug() << "Name Column";
            return "Test";
            return name;
        }

        case 1:
        {
            qDebug() << "Value Column";
            return node->getWeight();
        }
    }

    return QVariant();
}

QVariant ModelItemNeural::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        switch (section)
        {
            case 0:
            {
                return "Node";
            }
            case 1:
            {
                return "Weight";
            }
        }
    }

    return QVariant();
}

NeuralNode * ModelItemNeural::nodeFromIndex(const QModelIndex &index) const
{
    if (index.isValid() == true)
    {
        //return (NeuralNode*)(index.internalPointer());
        return static_cast<NeuralNode *>(index.internalPointer());
    }
    else
    {
        return rootNode;
    }
}

void ModelItemNeural::setRootNode(NeuralNode *rootNode)
{
    delete this->rootNode;
    this->rootNode = rootNode;
    reset();
}

来自MainWindow的视图所在的代码如下所示。

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    NeuralNetwork* network = new NeuralNetwork();
    modelNeural = new ModelItemNeural();
    modelNeural->setRootNode(network);
    ui->treeView->setModel(modelNeural);

    update();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionNew_triggered()
{
    NeuralNetwork* network = new NeuralNetwork();

    modelNeural->setRootNode(network);
    ui->treeView->update();
}

我应该提一下,标题会显示此模型。但是,即使我设置了一个项目,窗口小部件中也不会显示任何内容。

哦,NeuralNetworkNeuralNode的子。

c++ qt qtreeview qabstractitemmodel
3个回答
5
投票

问题是这个片段:

int ModelItemNeural::rowCount(const QModelIndex& parent) const
{
    if (parent.isValid() == false)
    {
        return 0;
    }

您基本上是说根节点(由无效的父索引表示)没有子节点,即模型的顶级行为零。所以视图不再查询。

只需放弃此检查即可。 nodeFromIndex似乎正确处理根节点。


0
投票

您是否将模型(而不是项目)添加到树视图控件中?您是否创建了派生类型的项目并将其添加到模型中?如果正在访问您的模型,则应调用data()。


0
投票

您必须在QAbstractItemModel继承的类中重写以下方法:

QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;

然后写在:

return createIndex(row, column, nullptr);

像这样的东西:

QModelIndex index(int row, int column, const QModelIndex &parent) const { return createIndex(row, column, nullptr); }

© www.soinside.com 2019 - 2024. All rights reserved.