QT实现windows系统文件目录生成以及删除工具

QT实现windows系统目录生成以及删除工具

1、mytreeview.hpp

#ifndef MYTREEVIEW_H
#define MYTREEVIEW_H

#include <QWidget>
#include <QtGui>
#include <QDirModel>
#include <QTreeView>
#include <QHBoxLayout>
#include <QPushButton>
#include <QInputDialog>
#include <QMessageBox>


class mytreeview : public QWidget
{
    Q_OBJECT

public:
    mytreeview(QWidget *parent = nullptr);
    ~mytreeview();
    
private:
    QDirModel *model;
    QTreeView *treeView;
    
private slots:
    void mkdir();
    void rm();

};
#endif // MYTREEVIEW_H

2、mytreeview.cpp

#include "mytreeview.h"
#include "ui_mytreeview.h"

mytreeview::mytreeview(QWidget *parent)
    : QWidget(parent)
{   
    model = new QDirModel;
    model->setReadOnly(false);
    model->setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::Name);
    
    treeView = new QTreeView;
    treeView->setModel(model);
    treeView->header()->setStretchLastSection(true);
    treeView->header()->setSortIndicator(0, Qt::AscendingOrder);
    treeView->header()->setSortIndicatorShown(true);
    //treeView->header()->setClickable(true);

    QModelIndex index = model->index(QDir::currentPath());
    treeView->expand(index);
    treeView->scrollTo(index);
    treeView->resizeColumnToContents(0);

    QHBoxLayout *btnLayout = new QHBoxLayout;
    QPushButton *createBtn = new QPushButton(tr("Create Directory..."));
    QPushButton *delBtn = new QPushButton(tr("Remove"));

    btnLayout->addWidget(createBtn);
    btnLayout->addWidget(delBtn);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(treeView);
    mainLayout->addLayout(btnLayout);
    this->setLayout(mainLayout);

    connect(createBtn, SIGNAL(clicked()), this, SLOT(mkdir()));
    connect(delBtn, SIGNAL(clicked()), this, SLOT(rm()));
}

mytreeview::~mytreeview()
{
    
}

void mytreeview::mkdir()
{
    QModelIndex index = treeView->currentIndex();
    if (!index.isValid())
    {
        return;
    }
    QString dirName = QInputDialog::getText(this,
                                            tr("Create Directory"),
                                            tr("Directory name"));
    if (!dirName.isEmpty())
    {
        if (!model->mkdir(index, dirName).isValid())
        {
             QMessageBox::information(this,
                                 tr("Create Directory"),
                                 tr("Failed to create the directory"));
        }
    }
}

void mytreeview::rm()
{
    QModelIndex index = 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("Failed to remove%1").arg(model->fileName(index)));
    }
}

效果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值