Search code examples
listpermissionsdirectoryqt5qdir

Cannot list device files in Ubuntu/Debian using QDir in Qt5


On Ubuntu/Debian platform I want to list all possible video sources using QDir in my Qt5 application by listing their device files under /dev.

The code below works fine if I set myPath to a dummy folder in in my home folder full of text files named video0, video1 etc. but once I try to list directly from /dev it does not list any files at all.

    QString myPath="/dev";// This works when I set it to my dummy folder /home/me/dev with dummy text files in it.
    QDir dir;
    dir.setPath(myPath);
    QStringList filters;
    filters<<"video?";
    dir.setNameFilters(filters);
    dir.setSorting(QDir::Name );
    QFileInfoList list = dir.entryInfoList();
    const int ct= list.size();
    qDebug()<<"Found " <<QString::number(ct)<<" cameras";
    for (int i = 0; i <ct; ++i) {
        QFileInfo fileInfo = list.at(i);
        qDebug()<<"Camera : "<<QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.fileName());

    }

I can cd /dev && ls just fine from the commandline so I know my user has access to list that dir. Also, there are no error messages or other indication that permission was denied. What is going on?


Solution

  • I found the answer in documentation literally seconds after posting my question and I feel a bit stupid now for not guessing it so here is the answer.

    //Search for all files including system files (system means device files on linux)
    dir.setFilter( QDir::AllEntries | QDir::System);
    

    From documentation:

    QDir::System 0x200 List system files (on Unix, FIFOs, sockets and device files are included; on Windows, .lnk files are included)