Paul Smith
Paul Smith

Reputation: 41

get child from parent in Qtreeview

I am developing a Qt application using QTreeView and QFileSystemModel. I am able to get the parent's child till one level but I am not able to get the parent's child's child. For eg:

C is child of B,

B is child of A

I am able to get B as A's child but I also want C as A's Child. I want like this C->B->A.

Can someone give some input regarding this and help me out. Thanks in advance.

//QItemSelectionModel *sel = ui->dir_tree->selectionModel();
QStringList strings = extractStringsFromModel(ui->dir_tree->model(), ui->dir_tree->rootIndex());

QFileSystemModel* model = (QFileSystemModel*)ui->dir_tree->model();
QModelIndexList indexlist = ui->dir_tree->selectionModel()->selectedIndexes();
QVariant data;

//QList<QModelIndex> modelindex(indexlist);

int row = -1;

for(int i=0; i<indexlist.size();i=i+4)
{
    QModelIndex mi=indexlist.at(i);
    info1 = model->fileInfo(mi);
    QString childstr = info1.filePath();
    QString childname = info1.fileName();

    QModelIndex mi2= indexlist.at(i).parent();
    info = model->fileInfo(mi2);
    QString parentstr = info.filePath();
    QString parentname = info.fileName();
    QStringList childlist;
    for(int j=0;j<model->rowCount(indexlist.at(i));j++)
    {
        QModelIndex mi3 = indexlist.at(i).child(j, 0);
        info2 = model->fileInfo(mi3);
        QString childrenstr = info2.filePath();
        childlist << childrenstr;
        qDebug()<<"parents' children"<<childrenstr<<j;
    }
}

Upvotes: 1

Views: 14772

Answers (3)

Carlton
Carlton

Reputation: 4297

I like to use recursion for this kind of thing:

void MyTreeView::GetAllChildren(QModelIndex &index, QModelIndexList &indicies)
{
    indicies.push_back(index);
    for (int i = 0; i != model()->rowCount(index); ++i)
        GetAllChildren(index.child(i,0), indicies);
}

Usage (from somewhere inside the tree view):

QModelIndexList list;
GetAllChildren(model()->index(0,0), list);

Upvotes: 3

user5096661
user5096661

Reputation: 1

//Delete
    QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
    QTreeWidgetItem *item = selected.first();
    QTreeWidgetItem *parent = item->parent();
    if(parent) {
        int index = parent->indexOfChild(item);
        delete parent->takeChild(index);
    }

//Add
QList<QTreeWidgetItem *> selected = ui->twg->selectedItems();
QTreeWidgetItem *item = selected.first();
QTreeWidgetItem *parent = item->parent();
if(parent) {
    QTreeWidgetItem *tmp = new QTreeWidgetItem(parent);
    tmp->setText(0, "Dude");
    parent->addChild(tmp);
}

Upvotes: 0

cmannett85
cmannett85

Reputation: 22346

Getting all children breadth-first:

QModelIndexList children;

//  Get top-level first.
for ( int i = 0; i < model->rowCount(); ++i ) {
    children << model->index( i, 0 );  //  Use whatever column you are interested in.
}

// Now descend through the generations.
for ( int i = 0; i < children.size(); ++i ) {
    for ( int j = 0; j < model->rowCount( children[i] ); ++j ) {
        children << children[i].child( j, 0 );
    }
}

Getting all parents from a child:

QModelIndexList parents;
parents << child.parent();
while ( parents.last().isValid() ) {
    parents << parents.last().parent();
}

Please note, I haven't tried these samples!

Upvotes: 1

Related Questions