try this code
{
connect(m_pb, SIGNAL(clicked()), SLOT(clearFolder()));
setCentralWidget(m_pb);
}
void MainWindow::clearFolder()
{
clearFolder("E:/f0");
}
void MainWindow
::clearFolder(const QString &folder
) {
if (fileInfo.
isFile() && !QFile::remove(fileInfo.
filePath())) { qWarning() << "can't remove '" << fileInfo.filePath() << "'";
return;
} else if (fileInfo.isDir())
clearFolder(fileInfo.filePath());
}
if (!dir.rmdir(dir.absolutePath()))
qWarning() << "can't remove '" << dir.absolutePath() << "'";
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_pb = new QPushButton(tr("Clear"));
connect(m_pb, SIGNAL(clicked()), SLOT(clearFolder()));
setCentralWidget(m_pb);
}
void MainWindow::clearFolder()
{
clearFolder("E:/f0");
}
void MainWindow::clearFolder(const QString &folder)
{
QDir dir(folder);
QStringList entries = dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
foreach (QString entry, entries) {
QFileInfo fileInfo(folder + "/" + entry);
if (fileInfo.isFile() && !QFile::remove(fileInfo.filePath())) {
qWarning() << "can't remove '" << fileInfo.filePath() << "'";
return;
} else if (fileInfo.isDir())
clearFolder(fileInfo.filePath());
}
if (!dir.rmdir(dir.absolutePath()))
qWarning() << "can't remove '" << dir.absolutePath() << "'";
}
To copy to clipboard, switch view to plain text mode
Bookmarks