Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:30:43+00:00 2026-05-24T05:30:43+00:00

I have followed the editableTreeView example provided with Qt and I’m having an interesting

  • 0

I have followed the editableTreeView example provided with Qt and I’m having an interesting problem. Top level items can be added correctly but if I am to give one of them a child, its a pointer to the first top level item.

My code for the QAbstractItemModel is below.

#include "ModelItemNeural.h"

ModelItemNeural::ModelItemNeural(QObject *parent)
    : QAbstractItemModel(parent)
{
    counta = 0;
    rootNode = new NeuralNode();
    addNeuralNode(NeuralNode::NEURAL_NETWORK, 0, 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();
//    }
    if (parent.isValid() && parent.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);
}

QVariant ModelItemNeural::data(const QModelIndex &index, int role) const
{
    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);
        }

        //return "Test";
        return node->getId();
        return name;
    }
    case 1:
    {
        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();
}

int ModelItemNeural::rowCount(const QModelIndex &parent) const
{
    NeuralNode *parentItem = nodeFromIndex(parent);

    return parentItem->childCount();
}
int ModelItemNeural::columnCount(const QModelIndex &parent) const
{
    return rootNode->columnCount();
}

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

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

Qt::ItemFlags ModelItemNeural::flags(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

bool ModelItemNeural::insertColumns(int position, int columns, const QModelIndex &parent)
{
    bool success;

    beginInsertColumns(parent, position, position + columns - 1);
    success = rootNode->insertColumns(position, columns);
    endInsertColumns();

    return success;
}

bool ModelItemNeural::removeColumns(int position, int columns, const QModelIndex &parent)
{
    bool success;

    beginRemoveColumns(parent, position, position + columns - 1);
    success = rootNode->removeColumns(position, columns);
    endRemoveColumns();

    if (rootNode->columnCount() == 0)
    {
        removeRows(0, rowCount());
    }

    return success;
}

bool ModelItemNeural::insertRows(int position, int rows, const QModelIndex &parent)
{
    NeuralNode *parentItem = nodeFromIndex(parent);
    counta++;
    bool success;

    beginInsertRows(parent, position, position + rows - 1);
    switch (addedNode)
    {
    case NeuralNode::NEURALNODE:
    {
    default:
        break;
    }
    case NeuralNode::NEURON:
    {
        success = parentItem->insertChildren(position, rows, new Neuron(NeuralNode::NEURON, counta + 100, counta));
        break;
    }
    case NeuralNode::NEURAL_NETWORK:
    {
        success = parentItem->insertChildren(position, rows, new NeuralNetwork());
        break;
    }
    case NeuralNode::SENSOR_INT:
    {
        success = parentItem->insertChildren(position, rows, new SensorInt());
        break;
    }
    case NeuralNode::SENSOR_DOUBLE:
    {
        success = parentItem->insertChildren(position, rows, new SensorDouble());
        break;
    }
    }
    endInsertRows();

    return success;
}

bool ModelItemNeural::removeRows(int position, int rows, const QModelIndex &parent)
{
    NeuralNode *parentItem = nodeFromIndex(parent);
    bool success = true;

    beginRemoveRows(parent, position, position + rows - 1);
    success = parentItem->removeChildren(position, rows);
    endRemoveRows();

    return success;
}

void ModelItemNeural::addNeuralNode(const NeuralNode::NeuralType& type, int position, int columns, const QModelIndex &parent)
{
    addedNode = type;
    if (columnCount(parent) == 0)
    {
        if (insertColumn(0, parent) == false)
        {
            return;
        }
    }

    if (insertRow(0, parent) == false)
    {
        return;
    }
    //insertRows(position, columns, parent);
}

void ModelItemNeural::removeNeuralNode(const NeuralNode::NeuralType& type, int position, int columns, const QModelIndex &parent)
{

}

The code for the NeuralNode (items for the tree) is shown below

#include "NeuralNode.h"

NeuralNode::NeuralNode(NeuralNode *parent)
    : id(0), type(NeuralNode::NEURALNODE), weight(0), parent(parent)
{
}

NeuralNode::NeuralNode(const NeuralType &type, NeuralNode *parent)
    : id(id), type(type), weight(0), parent(parent)
{
}

NeuralNode::NeuralNode(const NeuralType &type, const int &id, NeuralNode *parent)
    : id(id), type(type), weight(weight), parent(parent)
{
}

NeuralNode::NeuralNode(const NeuralType &type, const int &id, const double &weight, NeuralNode *parent)
    : id(id), type(type), weight(weight), parent(parent)
{
}

bool NeuralNode::operator ==(const NeuralNode &node) const
{
    if (this->id != node.id)        // The id of this Neuron.
    {
        return false;
    }
    else if (weight != node.weight) // The weight of this Neuron.
    {
        return false;
    }
    else if (inputs != node.inputs)   // The inputs to this NeuralNode.
    {
        return false;
    }
    else if (parent != node.parent) // The parent of the NeuralNode.
    {
        return false;
    }
    else
    {
        return true;
    }
}

NeuralNode * NeuralNode::getParent() const
{
    return parent;
}

void NeuralNode::setParent(NeuralNode *parent)
{
    this->parent = parent;
}

QList<NeuralNode*> NeuralNode::getInputs() const
{
    return inputs;
}

void NeuralNode::setInputs(const QList<NeuralNode*> &inputs)
{
    this->inputs = inputs;
}

NeuralNode * NeuralNode::child(int number)
{
    return inputs.value(number);
}

int NeuralNode::childCount() const
{
    return inputs.count();
}

int NeuralNode::columnCount() const
{
    return 2;
}

bool NeuralNode::insertChildren(int position, int count, NeuralNode* node)
{
    if (position < 0 || position > inputs.length())
    {
        return false;
    }

    for (int row = 0; row < count; ++row)
    {
       inputs.insert(position, node);
    }

    return true;
}

bool NeuralNode::removeChildren(int position, int count)
{
    if (position < 0 || position + count > inputs.length())
    {
        return false;
    }

    for (int row = 0; row < count; ++row)
    {
        delete inputs.takeAt(position);
    }

    return true;
}

int NeuralNode::childNumber() const
{
    return inputs.length();
}

bool NeuralNode::insertColumns(int position, int columns)
{
    if (position < 0)
    {
        return false;
    }

    for (int a = 0; a < inputs.length(); ++a)
    {
        inputs.at(a)->insertColumns(position, columns);
    }

    return true;
}

bool NeuralNode::removeColumns(int position, int columns)
{
    if (position < 0)
    {
        return false;
    }

    for (int a = 0; inputs.length(); ++a)
    {
        inputs.at(a)->removeColumns(position, columns);
    }

    return true;
}

Here is what i’ve noticed.

  • Tracing through the index() call from my model does return indices for children of top level nodes.
  • calling ui->treeView->selectedModel()->currentIndex() always returns a invalid index (root). The same is true for ui->treeView->currentIndex().

I smell a problem with a pointer somewhere but I can’t find it. Any help will be greatly appreciated.

Jec

Edit: Here is a crude graphic of the problem

Currently:

Root->A
      B
      C->A

What I Want:

Root->A
      B
      C->D

Where A, B, C and D are unique NeuralNodes. I should have no duplicates in the tree.

Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:30:44+00:00Added an answer on May 24, 2026 at 5:30 am

    it turns out of I forgot to link the children to their parents. Without a valid parent() the top level nodes are always added.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have followed this article and implemented my service and I can open the
I have followed the code example here toupper c++ example And implemented it in
I have followed the example in for the gradient dividers: http://www.connorgarvey.com/blog/?p=34 I have tried
I have followed this tutorial and added iAd to my app: http://bees4honey.com/blog/tutorial/how-to-add-iad-banner-in-iphoneipad-app/ But the
i have followed the railscasts episode on nested forms(part 1 and 2) and having
Have followed every step correctly from this link http://msdn.microsoft.com/en-us/library/ms746621.aspx When trying to localize the
I have followed the onTouch example from google located here . However, I get
I have followed the Facebook PHP SDK example and created the following. <?php require
I have followed the answer provided here to create a drop shadow for my
I have followed the code provided by Google concerning implemeting a Web Map Activity,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.