ZBLOG

QStringList fileList = folder.entryList(QDir::Files); qSort(fileList.begin(), fileList.end(), compareFileNames); 将 compareFileNames 改为sortFileNames

如果您想将 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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=12794

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?