QT C++ 常用的几个转换函数:string转QString(不会出现乱码现象);cv::Mat转QImage;删除/复制某个文件夹下所有的文件(包括子目录的文件);

1 篇文章 0 订阅

一、QString转string

QString str2qstr2(const std::string &str) {
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    if (codec) {
        QByteArray byteArray = codec->fromUnicode(QString::fromStdString(str));
        QString convertedString = codec->toUnicode(byteArray);
        return convertedString;
    }
    else {
        return QString::fromStdString(str);
    }
}

二、cv::Mat转QImage

QImage Mat2QImage(const cv::Mat& mat){

    // 8-bits unsigned, NO. OF CHANNELS = 1
    if(mat.type() == CV_8UC1) {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        // Set the color table (used to translate colour indexes to qRgb values)
        image.setColorCount(256);
        for(int i = 0; i < 256; i++) {
            image.setColor(i, qRgb(i, i, i));
        }
        // Copy input Mat
        uchar *pSrc = mat.data;
        for(int row = 0; row < mat.rows; row ++){
            uchar *pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    // 8-bits unsigned, NO. OF CHANNELS = 3
    else if(mat.type() == CV_8UC3 ){
        // Copy input Mat

        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    }
    else if(mat.type() == CV_8UC4) {
        // Copy input Mat
        const uchar *pSrc = (const uchar*)mat.data;
        // Create QImage with same dimensions as input Mat
        QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_ARGB32);
        return image.copy();
    }
    else {
        return QImage();
    }
}

三、删除/复制某个文件夹下所有的文件(包括子目录的文件)

1)删除

void deleteImages(int days,  QDir imageDir)
{
    // 获取当前日期
    QDateTime currentDate = QDateTime::currentDateTime();
    // 获取目录中的所有文件和文件夹
    QStringList entries = imageDir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);

    // 遍历文件和文件夹列表
    foreach (const QString& entry, entries)
    {
        QString fullPath = imageDir.filePath(entry);
        QFileInfo fileInfo(fullPath);

        if (fileInfo.isDir())  // 如果是文件夹,递归调用删除函数
        {
            QDir subDir(fullPath);
            deleteImages(days, subDir);
        }
        else  // 如果是文件,检查修改日期并删除
        {
            //qDebug() << fileInfo.fileName() << fileInfo.lastModified().daysTo(currentDate);
            if (fileInfo.lastModified().daysTo(currentDate) > days)
            {
                imageDir.remove(entry);
            }
        }
    }
}

2)复制

void copyRecursively(const QString &srcPath, const QString &dstPath) {

    QDir sourceDir(srcPath);
    QDir destinationDir(dstPath);

    if (!destinationDir.exists()) {
        destinationDir.mkpath(".");
    }

    QStringList files = sourceDir.entryList(QDir::Files);
    for (const QString &file : files) {
        QString srcFilePath = srcPath + "/" + file;
        QString dstFilePath = dstPath + "/" + file;
        QFile::copy(srcFilePath, dstFilePath);
    }

    QStringList dirs = sourceDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    for (const QString &dir : dirs) {
        QString srcDirPath = srcPath + "/" + dir;
        QString dstDirPath = dstPath + "/" + dir;
        copyRecursively(srcDirPath, dstDirPath);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值