如果您想将 compareFileNames
改为 sortFileNames
,则需要修改代码如下:
#include <QtCore>
#include <QDebug>
bool sortFileNames(const QString& s1, const QString& s2)
{
QRegularExpression re("(\\d+)_([RX]).tiff");
QRegularExpressionMatch match1 = re.match(s1);
QRegularExpressionMatch match2 = re.match(s2);
if (match1.hasMatch() && match2.hasMatch()) {
int number1 = match1.captured(1).toInt();
int number2 = match2.captured(1).toInt();
return number1 < number2;
}
return false;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QString folderPath = "YourFolderPath";
QDir folder(folderPath);
QStringList fileList = folder.entryList(QDir::Files);
qSort(fileList.begin(), fileList.end(), sortFileNames);
qDebug() << "Sorted Filenames:";
for (const QString& filename : fileList) {
qDebug() << filename;
}
return app.exec();
}
上述代码中的 sortFileNames
函数用于比较两个文件名并进行排序。您可以根据需要设置文件夹路径并运行代码来获取按照数字排序的文件列表。请确保替换 "YourFolderPath"
为实际的文件夹路径。
内容由零声教学AI助手提供,问题来源于学员提问