Results 1 to 3 of 3

Thread: Problem in displaying directories in Tree widget

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

    The code i wrote is:
    Qt Code:
    1. bool MainWindow::DisplayDirs(QDir dir)
    2. {
    3. std::cout<<"THE DIR DISPLAYING IS:"<<dir.dirName().toStdString()<<endl;
    4. bool ok=dir.exists();
    5. if(ok)
    6. {
    7. QFileInfoList dirContents=dir.entryInfoList();
    8. foreach(QFileInfo entryInfo,dirContents)
    9. {
    10. QString entryPath= entryInfo.absoluteFilePath();
    11. if(entryInfo.isDir())
    12. {
    13. QDir subDir(entryPath);
    14. AddRoot(subDir);
    15. }
    16.  
    17. }
    18. if(ok && !dir.exists(dir.absolutePath()))
    19. ok=false;
    20. return ok;
    21. }
    22. return ok;
    23.  
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void MainWindow::AddRoot(QDir dir)
    2. {
    3.  
    4. std::cout<<"NEED TO ADD ROOT DIR:"<<dir.dirName().toStdString()<<endl;
    5. QString name=dir.dirName();
    6. QTreeWidgetItem *itm =new QTreeWidgetItem(ui->treeWidget);
    7. itm->setText(0,name);
    8.  
    9. ui->treeWidget->addTopLevelItem(itm);
    10. QFileInfoList fileList=dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files );
    11.  
    12. foreach(QFileInfo entry, fileList)
    13. {
    14. if(entry.isFile())
    15. {
    16.  
    17. QString filename=entry.fileName();
    18. AddChild(itm,filename);
    19. cout<<"ADDEDE CHILD:"<<filename.toStdString()<<endl;
    20. }
    21. if(entry.isDir())
    22. {
    23. QDir subDir(entry.absoluteFilePath());
    24. AddRoot(subDir);
    25.  
    26. }
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::AddChild(QTreeWidgetItem *parent,QString name)
    2. {
    3. itm->setText(0,name);
    4.  
    5. parent->addChild(itm);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem in displaying directories in Tree widget

    Why don't you just use QFileSystemModel?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem in displaying directories in Tree widget

    Ya i tried using QFileSystemModel....but getting error...
    plz look at this---->http://www.qtcentre.org/threads/4730...586#post213586

Similar Threads

  1. tree view displaying widgets as items
    By paksas in forum Qt Programming
    Replies: 9
    Last Post: 28th January 2013, 13:58
  2. Problem with tree widget
    By keyurparekh in forum Qt Programming
    Replies: 4
    Last Post: 8th April 2011, 22:14
  3. tree widget like designer's widget tree
    By nroberts in forum Newbie
    Replies: 1
    Last Post: 20th November 2010, 00:06
  4. Return number of directories in a directories
    By franco.amato in forum Newbie
    Replies: 7
    Last Post: 29th September 2010, 23:29
  5. Return number of directories in a directories
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2010, 18:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.