一、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);
}
}