Problem in displaying directories in Tree widget



  • I am using a tree widget to display a file structure, directories , sub directories and files......
    But the sub directories are not displaying in proper hierarchy....
    Plz tell me the mistake i did here.

    @ bool MainWindow::DisplayDirs(QDir dir)
    {
    std::cout<<"THE DIR DISPLAYING IS:"<<dir.dirName().toStdString()<<endl;
    bool ok=dir.exists();
    if(ok)
    {
    QFileInfoList dirContents=dir.entryInfoList();
    foreach(QFileInfo entryInfo,dirContents)
    {
    QString entryPath= entryInfo.absoluteFilePath();
    if(entryInfo.isDir())
    {
    QDir subDir(entryPath);
    AddRoot(subDir);
    }

    }
    if(ok && !dir.exists(dir.absolutePath()))
    ok=false;
    return ok;
    }
    return ok;
     
     
    }
    

    @

    @ void MainWindow::AddRoot(QDir dir)
    {

    std::cout<<"NEED TO ADD ROOT DIR:"<<dir.dirName().toStdString()<<endl;
    QString name=dir.dirName();
    QTreeWidgetItem *itm =new QTreeWidgetItem(ui->treeWidget);
    itm->setText(0,name);
     
    ui->treeWidget->addTopLevelItem(itm);
    QFileInfoList fileList=dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files );
     
    foreach(QFileInfo entry, fileList)
    {
    if(entry.isFile&#40;&#41;)
    {
     
    QString filename=entry.fileName();
    AddChild(itm,filename);
    cout<<"ADDEDE CHILD:"<<filename.toStdString()<<endl;
    }
    if(entry.isDir())
    {
    QDir subDir(entry.absoluteFilePath());
    AddRoot(subDir);
     
    }
    }
     
    }@
    

    @ void MainWindow::AddChild(QTreeWidgetItem *parent,QString name)
    {
    QTreeWidgetItem *itm =new QTreeWidgetItem();
    itm->setText(0,name);

    parent->addChild(itm);
     
    }@


  • At glance it seems to me that you are adding each directory as top level item to the tree when you should add it as child of the parent level item. Is this your problem?



  • [quote author="fluca1978" date="1328772711"]At glance it seems to me that you are adding each directory as top level item to the tree when you should add it as child of the parent level item. Is this your problem?[/quote]

    Ya exactly.....
    What should i need to do, so that they added in proper hierarchy?



  • The AddRoot method should work as AddChild: accept an item to attach the subtree to.



  • even i tried using QFileSystem...but getting runtime error...
    Plz look at this post---->http://developer.qt.nokia.com/forums/viewthread/14343/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.