Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem in displaying directories in Tree widget

Problem in displaying directories in Tree widget

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    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);
     
    }@
    
    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aurora
        wrote on last edited by
        #3

        [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?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aurora
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved