Access to .h and .cpp files

39 Views Asked by At

I am trying to access .h and .cpp files in a selected folder. My goal is to find and print on MainWindow how many lines in .h and .cpp files. In my code, there is no problem to access to selected folder. But for example, what if a .txt file is in this folder, I do not want to count lines of .txt file. My code is here:

int countLine::funcCountLines(QString fullPath)
{

    int counter = 0;
    QFile file(fullPath);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return 0;
    }

    while(!file.atEnd())
    {
        QByteArray line = file.readLine();
        counter++;
    }
return counter;
}

Best regards!

1

There are 1 best solutions below

0
JarMan On

You can use QDir::setNameFilters to retrieve only the files you are interested in.

QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);

// Only find .h and .cpp files
QStringList filters;
filters << "*.cpp" << "*.h";
dir.setNameFilters(filters);

QFileInfoList list = dir.entryInfoList();

for (int i = 0; i < list.size(); ++i) {
    QFileInfo fileInfo = list.at(i);
    funcCountLines(fileInfo.fileName());
}