PDA

View Full Version : QDir::root ()



RenanBS
5th June 2014, 20:22
Hello,

I'm trying to get the root drive from my application. I'm using Windows 7 and I have 2 drives (C:\ and D:\). The drive C:\ is where my OS is installed and the drive D:\ is where my application is running.

This is a piece of code from my application:


QString m_app_dir = "D:/slave";
QDir dir;
dir.setPath (m_app_dir);




QString r = dir.root ().path ();

or


QString r = dir.root ().absolutePath ();


Both codes return C:\, is that the expected result?
If so, what would return D:\?

From Qt docs about root (): http://qt-project.org/doc/qt-5/qdir.html#root


Returns the root directory.
The directory is constructed using the absolute path of the root directory, ensuring that its path() will be the same as its absolutePath().


So I was assuming D:\ would be the return of the root() method.

Thanks,
Renan

anda_skoa
5th June 2014, 20:38
If you look at the method declaration of root() in the documentation, then you'll see that root() is a static method
So it does not use "dir" at all.

Since Windows does not have the concept of a unified root, the best thing the Windows implementation can return is C:\ since this basically is always the root of the system installation.

Cheers,
_

RenanBS
5th June 2014, 20:41
So, is there a way to get the drive from that path (d:\slave) or any other path (since I cannot assume it will always be d:\slave)?

sulliwk06
5th June 2014, 21:27
I suspect you'll have to get the current path and just extract the drive label from the string

Alundra
5th June 2014, 21:32
So, is there a way to get the drive from that path (d:\slave) or any other path (since I cannot assume it will always be d:\slave)?
Yea of course : String.left(indexOf('\') + 1);

RenanBS
5th June 2014, 21:47
Yeah, of course, that's what I didn't want to do. But ok, that's the solution anyway.
There could be a QDir method to do this.
How can I request such feature?

Thanks

Alundra
6th June 2014, 03:17
They will not accept it because it's a bad function.

RenanBS
6th June 2014, 14:08
They will not accept it because it's a bad function.
Well, that doesn't answer my question.

Check Poco::Path implementation, you can check it here: http://pocoproject.org/docs/Poco.Path.html

const std::string & getDevice() const; This method gives me exactly what I was asking.

Any way, thanks for all your answers.

wysota
6th June 2014, 14:33
So, is there a way to get the drive from that path (d:\slave) or any other path (since I cannot assume it will always be d:\slave)?

How about going up the directory tree in a loop until there is nowhere to go?


QDir getDrive(const QDir &path) {
QDir dir(path.canonicalPath());
while(dir.cdUp());
return dir;
}

Alundra
6th June 2014, 15:13
Looks like the more safe function can be, but this function has to be used carefully about performance.

RenanBS
6th June 2014, 18:00
How about going up the directory tree in a loop until there is nowhere to go?


QDir getDrive(const QDir &path) {
QDir dir(path.canonicalPath());
while(dir.cdUp());
return dir;
}

Really nice! I like that.
Thanks