qt自定义文件选择器

头文件

#ifndef FILE_INTERFACE_H
#define FILE_INTERFACE_H

#include <QWidget>
#include <QFileDialog>
#include <QMouseEvent>
#include <QProcess>
#include <QDebug>
#include <QListView>
#include <QTreeView>



namespace Ui {
class FileInterface;
}

class FileInterface : public QWidget
{
    Q_OBJECT
public:
    enum Mode
    {
        File = 0,
        Type1,
        Type1,
        Type1,
        Type1
        Type1,
        Type1,
        Type1,
        Type1
    };

public:
    explicit FileInterface(QWidget *parent = nullptr);
    ~FileInterface();
    void setMode(Mode mode);
    void onlyshow(QPoint point);


protected:
    void mouseMoveEvent(QMouseEvent *e) override;
    void mousePressEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    bool eventFilter(QObject *obj, QEvent *e) override;

private:
    void setFile();


signals:
    void ok_select(const QStringList &files);

public:


private:
    Ui::FileInterface *ui;
    QFileDialog *file_dialog;
    QPoint press_point;//鼠标按下去的点
    bool is_move;
    QString userPath;
    QString user;
    FileInterface::Mode mode;
};

#endif // FILE_INTERFACE_H

cpp文件
 

#include "file_interface.h"
#include "ui_file_interface.h"

FileInterface::FileInterface(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileInterface)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    this->setWindowIcon(QIcon(QApplication::applicationDirPath() + QString("/")  +  "image/.ico"));
    QProcess process;
    process.start("users");
    process.waitForFinished();
    QByteArray data = process.readAllStandardOutput();
    QList<QByteArray> data_list = data.split('\n');
    QList<QByteArray> tmp = data_list[0].split(' ');
    if(tmp.size() != 0)
    {
        userPath = QString("/home/") + tmp[0] + QString("/");
        user = tmp[0];
    }
    isUOS = false;
    QProcess process1;
    process1.start("cat /etc/issue");
    process1.waitForFinished();
    QByteArray data1 = process1.readAllStandardOutput();
    QList<QByteArray> data_list1 = data1.split('\n');
    if(data_list1.size() != 0 )
    {
        QList<QByteArray> tmp1 = data_list1[0].split(' ');
        if(tmp1.size() != 0)
        {
           isUOS = tmp1[0].contains("UnionTech");
        }
    }

    file_dialog = new QFileDialog(this, Qt::SubWindow | Qt::FramelessWindowHint);
    file_dialog->setAttribute(Qt::WA_TranslucentBackground);
    file_dialog->installEventFilter(this);
    file_dialog->setOption(QFileDialog::DontUseNativeDialog);
    file_dialog->setDirectory(userPath);


    QListView* listView = file_dialog->findChild<QListView*>("sidebar");
    if(listView)
    {
        listView->setVisible(false);
    }

    this->ui->verticalLayout->addWidget(file_dialog);
    this->ui->label_2->setPixmap(QPixmap::fromImage(QImage(QApplication::applicationDirPath() + QString("/")  +  "image/frame")));
    this->ui->label_2->setScaledContents(true);
    void (QFileDialog::*fileSelecteds)(const QStringList &files) = &QFileDialog::filesSelected;
    connect(file_dialog, fileSelecteds,[&](const QStringList &files){
        for(int i = 0; i < files.size(); i++)
        {
            QString file = files.at(i);
            if(!file.contains(userPath) && !file.contains(QString("/media/%1/").arg(user)) && !file.contains(QString("/media/")))
            {
		qDebug()<< "系统文件不支持操作";
);
                this->close();
                return;
            }
        }  
        emit ok_select(files);
        this->close();
    });
    connect(file_dialog, &QFileDialog::rejected,[&](){
        this->close();
    });
    connect(file_dialog, &QFileDialog::directoryEntered,[&](QString path){
        if(!path.contains(userPath) && (!path.contains(QString("/media/%1/").arg(user)) && !path.contains(QString("/media/"))))
        {
            file_dialog->setDirectory(userPath + path);
        }
        if(this->ui->label->text() == "保存位置")
        {
            QString Path = path + QString("/key.bin");
            QString keyname = QString("key.bin");
            int i = 1;
            while (QFile::exists(Path))
            {
                Path = path + QString("/key(%1).bin").arg(i);
                keyname = QString("key(%1).bin").arg(i);
                if(!QFile::exists(Path))
                {
                    break;
                }
                i++;
            }
            file_dialog->selectFile(keyname);
        }
        else if(this->ui->label->text() == "保存位置")
        {

        }
    });

}

FileInterface::~FileInterface()
{
    delete file_dialog;
    file_dialog = nullptr;
    delete ui;
}

void FileInterface::setMode(FileInterface::Mode mode)
{
    this->mode = mode;
    switch (mode)
    {
    case FileInterface::Mode::Test1:
        file_dialog->setFileMode(QFileDialog::ExistingFiles);
        this->ui->label->setText("选择一个或多个文件");
        this->setWindowTitle(QString("选择一个或多个文件"));
        file_dialog->setWindowTitle("选择一个或多个文件");
        break;
    case FileInterface::Mode::Test2:
        file_dialog->setFileMode(QFileDialog::Directory);
        file_dialog->setOption(QFileDialog::ShowDirsOnly);
        this->ui->label->setText("选择一个文件夹");
        this->setWindowTitle(QString("选择一个文件夹"));
        file_dialog->setWindowTitle("选择一个文件夹");
        break;
    }
}

void FileInterface::onlyshow(QPoint point)
{
    {
        this->move(point);
        this->show();
    }
}


void FileInterface::setFile()
{
    QString Path = userPath + QString("/key.bin");
    QString keyname = QString("key.bin");
    int i = 1;
    while (QFile::exists(Path))
    {
        Path = userPath + QString("/key(%1).bin").arg(i);
        keyname = QString("key(%1).bin").arg(i);
        if(!QFile::exists(Path))
        {
            break;
        }
        i++;
    }
    file_dialog->selectFile(keyname);
}



void FileInterface::setNameCard()
{
    QString Path = 
    QString name = 
    int i = 1;
    while (QFile::exists(Path))
    {
        Path = 
        name = QString("名字(%1).后缀").arg(i);
        if(!QFile::exists(Path))
        {
            break;
        }
        i++;
    }
    file_dialog->selectFile(name);
}

void FileInterface::mouseMoveEvent(QMouseEvent *e)
{
    if((e->buttons() == Qt::LeftButton) && is_move)
    {
        QWidget::mousePressEvent(e);
        QPoint mouseP = e->globalPos();
        QPoint endP(mouseP.x() - press_point.x(), mouseP.y() - press_point.y());
        this->move(endP);
    }
}

void FileInterface::mousePressEvent(QMouseEvent *e)
{
    press_point = QPoint();
    QWidget::mousePressEvent(e);
    QPoint mouseP = e->globalPos();
    int x = this->geometry().topLeft().x();
    int y = this->geometry().topLeft().y();
    int x_w = mouseP.x() - x;
    int y_w = mouseP.y() - y;
    press_point.setX(x_w);
    press_point.setY(y_w);
    is_move = true;
}

void FileInterface::mouseReleaseEvent(QMouseEvent *e)
{
    if(is_move)
    {
        is_move = false;
    }
}

bool FileInterface::eventFilter(QObject *obj, QEvent *e)
{

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值