PDA

View Full Version : QFilesystemmodel can not remove any folder or file?



vanduongbk
9th November 2013, 02:25
hello all
i have write a demo code to display and remove with QFileSystemModel ,but I can only display good that can not remove any folder
plz help me
this is my demo code

I init QFileSystemModel and QTreeView as belows:

QString sPath="D:/";
model= new QFileSystemModel(this);
model->setRootPath(sPath);
model->setReadOnly(false);
model->setFilter(QDir::Dirs|QDir::NoDotAndDotDot);

//assign property for model
ui->treeView->setModel(model);
ui->treeView->header()->setStretchLastSection(true);
ui->treeView->header()->setSortIndicator(1,Qt::DescendingOrder); //order with descend
ui->treeView->header()->setSortIndicatorShown(true);
ui->treeView->header()->setClickable(true);

//set hide some parametter
ui->treeView->setColumnHidden(1,true);
ui->treeView->setColumnHidden(2,true);
ui->treeView->setColumnHidden(3,true);
ui->treeView->setColumnHidden(4,true);

QModelIndex index=model->index(sPath);
ui->treeView->expand(index);
ui->treeView->scrollTo(index);
ui->treeView->setCurrentIndex(index);
ui->treeView->resizeColumnToContents(0);


and a code for a remove button as:

QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid())
{
return;
}
bool ok;
if (model->fileInfo(index).isDir())
{
ok=model->rmdir(index);
}
else
{
ok=model->remove(index);
}
if (!ok)
{
QMessageBox::information(this, tr("Remove"), tr("Can not remove %1").arg(model->fileName(index)));
}


with a remove button code above ,I only can remove with 1-2 times ,and can not execute it in next times
thank help me,thank