Results 1 to 11 of 11

Thread: QDir::root ()

  1. #1
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default QDir::root ()

    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:
    Qt Code:
    1. QString m_app_dir = "D:/slave";
    2. QDir dir;
    3. dir.setPath (m_app_dir);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QString r = dir.root ().path ();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QString r = dir.root ().absolutePath ();
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDir::root ()

    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,
    _

  3. #3
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QDir::root ()

    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)?

  4. #4
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDir::root ()

    I suspect you'll have to get the current path and just extract the drive label from the string

  5. #5
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDir::root ()

    Quote Originally Posted by RenanBS View Post
    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);

  6. #6
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QDir::root ()

    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

  7. #7
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDir::root ()

    They will not accept it because it's a bad function.

  8. #8
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QDir::root ()

    Quote Originally Posted by Alundra View Post
    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
    Qt Code:
    1. const std::string & getDevice() const;
    To copy to clipboard, switch view to plain text mode 
    This method gives me exactly what I was asking.

    Any way, thanks for all your answers.
    Last edited by RenanBS; 6th June 2014 at 14:13.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDir::root ()

    Quote Originally Posted by RenanBS View Post
    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?

    Qt Code:
    1. QDir getDrive(const QDir &path) {
    2. QDir dir(path.canonicalPath());
    3. while(dir.cdUp());
    4. return dir;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDir::root ()

    Looks like the more safe function can be, but this function has to be used carefully about performance.

  11. #11
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QDir::root ()

    Quote Originally Posted by wysota View Post
    How about going up the directory tree in a loop until there is nowhere to go?

    Qt Code:
    1. QDir getDrive(const QDir &path) {
    2. QDir dir(path.canonicalPath());
    3. while(dir.cdUp());
    4. return dir;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Really nice! I like that.
    Thanks

Similar Threads

  1. debugging as root
    By weaver4 in forum Qt Programming
    Replies: 0
    Last Post: 13th February 2012, 21:10
  2. Find the Qt root directory
    By sajis997 in forum Newbie
    Replies: 1
    Last Post: 15th July 2011, 22:52
  3. how to run QT application with root permission
    By noa l in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2010, 13:32
  4. label on root window
    By Corinzio in forum Qt Programming
    Replies: 0
    Last Post: 21st January 2009, 21:08
  5. Replies: 2
    Last Post: 2nd December 2008, 05:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.