8

I'm attempting to create a QTreeView and use a custom model for it. I have placed qDebug() statements at various places, and I have determined that data() is never being called. How can I fix this problem?

The model's code is below

#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();
}

The code from the MainWindow where the view is located is below.

#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();
}

I should mention that the header does display for this model. However, even when I set an item, nothing is displayed in the widget save the header.

Oh and NeuralNetwork is a sub of NeuralNode.

4

3 回答 3

5

The problem is this fragment:

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

You're basically saying that the root node (indicated by invalid parent index) has zero children i.e. the model has zero top-level rows. So the view queries no further.

Just drop this check and it should work. nodeFromIndex seems to handle root node correctly.

于 2011-07-23T23:02:41.237 回答
0

Did you add the model (not the item) to the treeview control? Did you create items of your derived type and add them to the model? data() should be called if your model is being accessed.

于 2011-07-23T21:03:10.750 回答
0

You have to override the following method in your QAbstractItemModel inherited class:

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

Then write this within:

return createIndex(row, column, nullptr);

Something like this:

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

于 2019-03-08T15:42:05.817 回答