aurora
9th February 2012, 05:34
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:
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())
{
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);
}
But the sub directories are not displaying in proper hierarchy....
Plz tell me the mistake i did here.
The code i wrote is:
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())
{
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);
}