我写了个程序,来递归地删除某个文件夹,及其下的所有文件夹及文件。有以下四种情况:
1、父文件夹下只有若干子文件。能正常删除。
2、父文件夹下有一个子文件夹。能正常删除。
3、父文件夹下有一个子文件夹和多个文件。只有删除多个子文件,不能删除子文件夹和父文件夹。
4、父文件夹下有多子个文件夹。只有删除一个子文件夹,其余子文件夹和父文件夹不能删除。
程序如下:
model = new QDirModel ;
list = new QListView;
list->setModel(model);
void Imagewindow::rmfile(QModelIndex &index)
{
QModelIndex child_index;
int column, row, i, j;
model->setReadOnly(false);
if(!model->isDir(index))
{
model->remove (index);
}
else
{
if(model->hasChildren(index))
{
qDebug("has children!");
column = model->columnCount(index);
row = model->rowCount(index);
qDebug("column = %d, row = %d!",column, row);
for(i = 0; i <column ; i++)
for(j = 0; j <row ; j++)
{
child_index = model->index(j,i,index);
if(child_index.isValid())
{
filename = model->fileName(child_index);
qDebug("name = %s!",qPrintable(filename));
rmfile(child_index) ;
}
}
}
model->rmdir (index);
}
}