Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStorageInfo not giving mount path



  • i have latest qt and i am in linux.

    this is my code.

        QStringList result;
        const auto allMounted = QStorageInfo::mountedVolumes();
        result.reserve(allMounted.size());
        for(auto& singleMounted : allMounted)
        result << singleMounted.displayName();
    
        for(int i=0; i<result.count(); ++i ){
            QString l;
            l = result.at(i);
            QStorageInfo(l).rootPath();
            qDebug()<<QStorageInfo(l).rootPath();
        }
    
        qDebug()<< allMounted;
        qDebug()<< result;
    
    

    here is the output

    "/run"
    "/"
    "/tmp"
    "/run/user/1000"
    ""
    ""
    ("/run", "/", "/tmp", "/run/user/1000", "Storage", "Fast")
    

    why i am not getting last 2 storage mount path ?

    i also want to remove ( "/run", "/tmp", "/run/user/1000") this three.

    so please tell me how.


  • Lifetime Qt Champion

    Hi
    You could just use
    http://doc.qt.io/qt-5/qlist.html#removeAt
    and remove from list ?



  • and why i am not getting last 2 storage mount path ?


  • Lifetime Qt Champion

    @saber
    no idea. If you look in the linux - what are they ?
    maybe they dont have names at all and hence the empty strings.



  • @mrjj

    see the second debug output.

    last two is the name("Storage", "Fast"). but when i put them in here (QStorageInfo(l).rootPath() ) as "l" ,i am not getting the root path.


  • Lifetime Qt Champion

    @saber
    Maybe they are not rootPaths ?



  • @mrjj i found the solution

    QStringList result;
        const auto allMounted = QStorageInfo::mountedVolumes();
        result.reserve(allMounted.size());
        for(auto& singleMounted : allMounted)
        result << singleMounted.rootPath();
    
        for(int i=0; i<result.count(); ++i ){
            QString l = result.at(i);
            if((l == "/run/user/1000" )| l == "/tmp" | l == "/run"){
                result.removeAt(i);
            }
        }
    

    output

    ("/", "/run/user/1000", "/run/media/shaber/Storage", "/run/media/shaber/Fast")
    

    but this ("/run/user/1000") is not removed automatically .
    why is it ? and how to do remove it?


  • Lifetime Qt Champion

    @saber said in QStorageInfo not giving mount path:

    if((l == "/run/user/1000" )| l == "/tmp" | l == "/run"){
    result.removeAt(i);
    }

    What happens if you replace that with:

    if (l == "/run/user/1000" || l == "/tmp" || l == "/run") {
      result.removeAt(i);
    }
    


  • @aha_1980
    nothing changed

    ("/", "/run/user/1000", "/run/media/shaber/Storage")
    

    same.

    is there any way to get just the mounted REAL partition .


  • Lifetime Qt Champion

    @saber: now I see it. if you use a debugger, you will notice the problem too.

    you have a loop over x elements and compare element i against a pattern. if it matches, you remove the element.

    But: therefore all following elements shift a position forward, which means you will miss the next element when the loop continues.

    you can e.g. fix this by copying all wanted elements into a new list instead.

    Regards



  • @aha_1980
    Rather than copying to a new list, the simplest pattern to use when removing items from a list via a for () loop counter so as not to worry about shifting is always to count downward! :

    for (int i = result.count() - 1; i >= 0; --i )
        if (...)
            result.removeAt(i);
    

    will work :)


  • Lifetime Qt Champion

    @JonB

    Rather then removing elements from a list he could just add the elements he really wants, by adding the check here

    for(auto& singleMounted : allMounted)
        result << singleMounted.rootPath();
    

    Sorry, could not resist ;)



  • @aha_1980
    Yes, but of course only works if you're copying/outputting the desired items somewhere. If you need to remove from a list, the downward pattern is worth knowing :)



  • my final solution

     QStringList result;
        const auto allMounted = QStorageInfo::mountedVolumes();
        result.reserve(allMounted.size());
        for(auto& singleMounted : allMounted)
        result << singleMounted.rootPath();
    

Log in to reply