代码笔记:Qt无边框和目录的一些处理

做个代码笔记,下次直接复制就用了,不用再找了。我这个记忆力,也记不住几天、、、

1.无边框拖动:

1)设置无边框:

this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
//设置无边框,第一个参数是设置无边框。第二个参数是允许任务栏按钮右键菜单,第三个参数是允许最小化与还原。

2)定义需要的保存状态的变量,在类成员里。

QPoint mousePoint;//记录鼠标位置
bool mousePressed = false;//是否一直按着左键

3)重载三个事件实现无边框拖动:

void *::mouseMoveEvent(QMouseEvent *e)
{//如果是只点击部分区域拖动的话,就在move前判断区域
    if (mousePressed && (e->buttons() && Qt::LeftButton))
    {
        this->move(e->globalPos() - mousePoint);
        e->accept();
    }
}

void *::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton)
    {
         mousePressed = true;
         mousePoint = e->globalPos() - this->pos();
         e->accept();
    }
}

void *::mouseReleaseEvent(QMouseEvent * /*e*/)
{
    mousePressed = false;
}

其中*是你的类名。

2.无边框窗口阴影:

1)设置最底层的Widget透明显示:

this->setAttribute(Qt::WA_TranslucentBackground); //设置透明

2)拖一个真正布局的Widget放到最底层widget之上,设好位置,留下的缝隙就是窗口的阴影显示的区域。

3)重载paintEvent事件,画出来阴影:

void *::paintEvent(QPaintEvent * /*e*/)
{
    QPainterPath path;
    path.setFillRule(Qt::WindingFill);
    path.addRect(10, 10, this->width()-20, this->height()-20);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.fillPath(path, QBrush(Qt::white));

    QColor color(0, 0, 0, 50);
    for(int i=0; i<10; i++)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2);
        color.setAlpha(150 - qSqrt(i)*50);
        painter.setPen(color);
        painter.drawPath(path);
    }
}

 3.Qt递归复制目录:

//Qt复制目录的实现
bool CopyDir(const QDir &fromDir, const QDir &toDir, bool coverFileIfExist)
{
    QDir sourceDir = fromDir;
    QDir targetDir = toDir;
    if(!targetDir.exists())
    {    //如果目标目录不存在,则进行创建
        if(!targetDir.mkdir(toDir.absolutePath()))
            return false;
    }

    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;

        if(fileInfo.isDir())
        {    //当为目录时,递归的进行copy
            if(!CopyDir(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()),coverFileIfExist))
                return false;
        }
        else
        {   //当允许覆盖操作时,将旧文件进行删除操作 
            if(coverFileIfExist && targetDir.exists(fileInfo.fileName()))
            {
                targetDir.remove(fileInfo.fileName());
            }

            // 进行文件copy
            if(!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){
                return false;
            }
            //文件重命名到新目录,也就是移动文件。应该不支持垮分区的
          //   if(!QFile::rename(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){
          //      return false;
            }
        }
    }
    return true;
}

 4.Qt递归删除目录:

//Qt删除目录
bool DeleteDir(const QString & dirName)
{
    QDir dir(dirName);
    QString tmpdir = "";
    if(!dir.exists())
    {
        return false;
    }
    QFileInfoList fileInfoList = dir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;

        if(fileInfo.isDir())
        {
            tmpdir = dirName + ("/") + fileInfo.fileName();
            DeleteDir(tmpdir);
            dir.rmdir(fileInfo.fileName()); // 移除子目录 
        }
        else if(fileInfo.isFile())
        {
            dir.remove(fileInfo.fileName()); // 删除临时文件 
        }
    }
    dir.cdUp();            // 返回上级目录,因为只有返回上级目录,才可以删除这个目录 
    if(dir.exists(dirName))
    {
        if(!dir.rmdir(dirName))
            return false;
    }
    return true;
}

 

2 thoughts on “代码笔记:Qt无边框和目录的一些处理”

Leave a Reply to 康伟意 Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.