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 8738177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:41:04+00:00 2026-06-13T10:41:04+00:00

I have the following simple model example: #include <QtGui/QApplication> #include <QtGui/QTreeView> #include <QAbstractItemModel> class

  • 0

I have the following simple model example:

#include <QtGui/QApplication>
#include <QtGui/QTreeView>
#include <QAbstractItemModel>

class TestModel : public QAbstractItemModel
{
public:
    TestModel()
    {
        SetUpData();
    }

    virtual QModelIndex index(int row, int column, const QModelIndex& parent) const
    {
        if ( parent.isValid())
        {
            // child
            return createIndex( row, column, (void*)&mData[row].mChildren[column]);
        }
        // root
        return createIndex( row, column, (void*)&mData[row]);
    }

    virtual QModelIndex parent(const QModelIndex& child) const
    {
        ModelData* data = (ModelData*)child.internalPointer();

        // find out where "data" is in the mData vector structure
        for ( size_t i=0; i<mData.size(); ++i )
        {
            for ( size_t j=0; j<mData[i].mChildren.size(); ++j )
            {
                if ( &mData[i].mChildren[j] == data )
                {
                    // I think this is correct, return the parent row at col 0?
                    return createIndex( i, 0, (void*)&mData[i].mChildren[j]);
                }
            }
        }
        return QModelIndex();
    }

    virtual int rowCount(const QModelIndex& parent) const
    {
        if ( parent.isValid() )
        {
            // Per root node size
            return mData[parent.row()].mChildren.size();
        }

        // Root size
        return mData.size();
    }

    virtual int columnCount(const QModelIndex& parent) const
    {
        // The "parent" nodes should have two columns, the children should have 1
        if ( parent.isValid() )
        {
            // Root
            return 1;
        }
        // Children
        return 2;
    }

    virtual QVariant data(const QModelIndex& index, int role) const
    {
        if ( role == Qt::DisplayRole && index.isValid() )
        {
            if ( index.isValid() )
            {
                // I think col and row are the wrong way around, but will crash if swapped
                return mData[index.column()].mChildren[index.row()].mName;
            }
            else
            {
                // this never happens because "RootN" is never displayed
                return mData[index.column()].mName;
            }
        }
        return QVariant();
    }

private:
    // The "real" data that this Qt model is providing an interface to
    struct ModelData
    {
        QString mName;
        std::vector< ModelData > mChildren;
    };

    std::vector< ModelData > mData;

    void SetUpData()
    {
        for ( int i=0; i<3; ++i )
        {
            ModelData root;
            root.mName = "Root" + QString::number( i+1 );

            for ( int j=0; j<10; ++j )
            {
                ModelData node;
                node.mName = "Node" + QString::number( j+1 );
                root.mChildren.push_back( node );
            }
            mData.push_back( root );
        }
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    TestModel* model = new TestModel();
    QTreeView* tv = new QTreeView();

    tv->setModel( model );
    tv->show();

    int ret = a.exec();

    delete tv;
    delete model;

    return ret;
}

What I’m trying to do here is create a model which has the following structure:

Root1
 |-Node1-10
 |
Root2
 |-Node1-10
 |
Root3
 |-Node1-10

However I end up with this:

output

I’m really confused about how index/parent and data are supposed to be working. Obviously I don’t understand it because my output is incorrect.

I thought that for example, Root1, Node3 would cause index to be called with row = 0, col=2, at which point I call createIndex with 0,2 and the pointer to that element?

And for parent() return QModelIndex if parent is valid as this means its a RootN element, then return createIndex to create an index to the parent if its not a root element.

Finally for data I figured this just returns the display string for the UI for the given row/column?

  • 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-06-13T10:41:05+00:00Added an answer on June 13, 2026 at 10:41 am

    I thought that for example, Root1, Node3 would cause index to be
    called with row = 0, col=2, at which point I call createIndex with 0,2
    and the pointer to that element?

    No. Your tree has only two columns: col 0 shows the tree hierarchy, and col 1 displays the labels again. So calling col 2 is just invalid.

    Root1, Node3 should have a row=2 col=(1/2, whichever you wanted) and a parent index equal to row=0, col=(1/2) with an invalid parent (an invalid parent means you are at root level).

    What data(..) returns depends on what role is passed to it, see the documentation for types you should be interested in.

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

Sidebar

Related Questions

Let's say we have the following simple model: public class Car { public int
Take the following simple object model for example: class Course has_many :enrollments has_many :students,
I have a simple model forms, that is being rendered through the following jinja2
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have the following simple class definition: //mycommon.h @interface CurrentPath : NSObject @property (nonatomic,
imagine the following simple Models (example for simplicity reasons; in fact, we have MVVM
I have the following django model (mapped to table 'A'): class A(models.Model): name =
I have a simple JSF 2.0 composite component example. <!DOCTYPE html PUBLIC -//W3C//DTD XHTML
Say you have the following object: public class Address { public String Line1 {
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):

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.