先看下效果:
点击list会显示当前目录下的所有文件,点击delete会删除选择的文件
下面是代码的实现:
void MainWindow::on_listButton_clicked()
{ //这是点击list按钮的实现
QDir *dir = new QDir(QDir::currentPath());
ui->listWidget->clear();
QStringList list = dir->entryList();
QString string;
/*for (int index = 0; index < list.size(); index++)
{
string = list.at(index);
//QListWidgetItem *item = new QListWidgetItem(string);
//ui->listWidget->addItem(item);
ui->listWidget->addItem(string);
}*/
ui->listWidget->addItems(list);
delete dir;
}
//delete按钮的实现
void MainWindow::on_deleteButton_clicked()
{
QListWidgetItem *item = ui->listWidget->currentItem();
QString filename = item->text();
//qDebug()< QDir curDir(QDir::currentPath());
QString curPath(curDir.path());
curPath.append("/");
curPath.append(filename);
//qDebug()< QFileInfo info(curPath);
if (info.isDir())
{
QDir delDir(curPath);
delDir.rmdir(curPath);
}
if (info.isFile())
{
QFile delFile(curPath);
delFile.remove();
}
this->on_listButton_clicked();
}
//这是双击列表条目时显示一个message对话框
void MainWindow::DoubleClicked(QListWidgetItem *item)
{
QString filename = item->text();
QMessageBox::information(this, "the select is ", filename);
}
阅读(4395) | 评论(0) | 转发(1) |