About filter file name in Qt



  • I have just problem as:
    I want filter all file have shape: "Exc1.zip" "Exc2.zip" v...v.. from a folder have many file.
    my code follow :
    @
    QRegExp re;
    re.setPatternSyntax(QRegExp::Wildcard);
    if(sTenThuMuc == "Challenge")
    {
    re.setPattern("Exc([0-9]{1,1}).zip");
    }
    else if(sTenThuMuc == "HangMan")
    {
    re.setPattern("hangman([0-9]+).zip");
    }
    @
    then in filter filename:
    @ QString sFilter = re.pattern();
    QStringList slFilters;
    slFilters << sFilter;
    qDir.setNameFilters(slFilters);
    QFileInfoList lFileInfo = qDir.entryInfoList();@

    plz help me!


  • Moderators

    Documentation states, that ::setNameFilters() understands wildcards, not full regular expressions.

    So, pass QString("Exc?.zip") to qDir.setNameFilters(), and then filter out files you need manually using QRegExp.



  • You are making it way to difficult. You are trying to use a full-blown regular expression, but QDir only understands simply wildcard matching:

    @
    QStringList filter;
    if(sTenThuMuc == "Challenge")
    {
    filter << "Exc?.zip";
    }
    else if(sTenThuMuc == "HangMan")
    {
    filter << "hangman*.zip";
    }

    // use the filter
    slFilters << sFilter;
    qDir.setNameFilters(filters);
    QFileInfoList lFileInfo = qDir.entryInfoList();
    //check if the returned file names actually match what you were after, because the filter matches more than just numbers at the wildcard positions!
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.