QAbstractItemModel data () НИКОГДА не вызывается

Я пытаюсь создать 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();
}

Я должен упомянуть, что заголовок отображает для этого модель. Однако даже когда я устанавливаю элемент, в виджете ничего не отображается, за исключением заголовка.

Ой, и NeuralNetwork является частью NeuralNode .

7
задан Brian Tompsett - 汤莱恩 21 November 2015 в 16:41
поделиться