Link to home
Start Free TrialLog in
Avatar of Jacob
JacobFlag for Denmark

asked on

Traversing thru directory structure for files

Hello experts.

I started C++ with Qt two days ago, and I am stock in trying to traverse thru my directory structure for MP3 files. I have found something and worked a little on that, I now have this;

void tjekdir(){
    QDir dir;
    dir.setPath("/home/you/Music/Some Artist");
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks | QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Dirs);
dir.setSorting(QDir::Size | QDir::Reversed);
    QFileInfoList list = dir.entryInfoList();
    std::cout << "   Extension Path Bytes" << std::endl;
    for (int i = 0; i < list.size(); ++i) {
    QFileInfo fileInfo = list.at(i);
    std::cout << qPrintable(QString("%1 %2 %3")
.arg(fileInfo.suffix())
.arg(fileInfo.absoluteFilePath())
.arg(fileInfo.size(), 10));

    std::cout << std::endl;
    }    
} 

Open in new window


As you see the code only find the files in that specific given directory, it does'nt go thru the structure what is what I want.
Can anyone help me further with this, thank you!

Jacob
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Have you looked at QDirIterator? I think it might help you.
Something like below should work for you :
QDirIterator it(dir, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
    qDebug() << it.next();
}
http://doc.qt.io/qt-5/qdiriterator.html
Avatar of Jacob

ASKER

Hello, thanks for your suggestion,

I get an error   at the line here: QDirIterator it(dir, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);

This error message:
no matching function for call to 'QDirIterator::QDirIterator(QDir&, QStringList&, QDir::Filter, QDirIterator::IteratorFlag)'
     QDirIterator it(dir, QStringList() << "*.mp3", QDir::Files, QDirIterator::Subdirectories);

I am not sure why, or what to do..
What can I do?
Have you included #include <QDirIterator>?
Also which version of Qt are you using?
Avatar of Jacob

ASKER

Yes I have added the #include

I have Qt Creator 3.5.1
Can you please write below code and let me know the Qt version?
qDebug() << QT_VERSION_STR;
Qt creator version doesn't help.
Avatar of Jacob

ASKER

it says

5.5.1
Also can you please navigate to the the header file QDirIterator (go to the source of QDirIterator header file) and let me know what constructors does it have? Also does the compilation error message more than saying no match, does it say what could be the possible candidates?
Give me few minutes, I shall send you a working code with inline comments.
ASKER CERTIFIED SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacob

ASKER

Okay. Thankyou. I got it working. Nice of you to give me two choices. I hope we will meet again, here on this site..
Thanks.!

Jacob
Glad I was of some help. Thanks, and you are welcome.