Solved QStorageInfo not giving mount path
-
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.
-
@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? -
@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 .
-
@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 afor ()
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 :)
-
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();