Results 1 to 7 of 7

Thread: QDir and problems with #ifdef OS

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDir and problems with #ifdef OS

    QT 4.7.0 (64bit)
    Slackware 13.1 (actually it's -current though wasn't sure if someone would be "what version is -current???")

    I'm trying to make a simple function that will test if folder of a certain program exists and I'm getting a problem that I'm not sure how to deal with.

    Qt Code:
    1. QString Program::ProgramFound()
    2. {
    3. QDir dir();
    4. QString location[4] = {"No", "C:/Program Files/Program", "C:/Program Files(x86)/Program", "~/.wine/drive_c/Program Files/Program"};
    5. #ifdef Q_MS_WIN //case of windows
    6. if (dir.exists(location[1])) //case it's 32 bit
    7. return location[1];
    8. if (dir.exists(location[2])) //case it's 64 bit
    9. return location[2];
    10. #endif
    11. #ifdef Q_WS_X11 //case of linux -> this one gives me problems
    12. if (dir.exists(location[3]))
    13. return location[3];
    14. #endif
    15. return location[0];
    16. }
    To copy to clipboard, switch view to plain text mode 

    I get for line 12 following error:
    error: request for member 'exists' in 'dir', which is of non-class type 'QDir()'
    At global scope:
    Thought that QDir in this case doesn't exit the scope of first #ifdef which was kinda weird considering that I defined it outside of it, so I tried to place QDir dir in both of those:

    Qt Code:
    1. QString Program::ProgramFound()
    2. {
    3. QString location[4] = {"No", "C:/Program Files/Program", "C:/Program Files(x86)/Program", "~/.wine/drive_c/Program Files/Program"};
    4. #ifdef Q_MS_WIN //case of windows
    5. QDir dir();
    6. if (dir.exists(location[1])) //case it's 32 bit
    7. return location[1];
    8. if (dir.exists(location[2])) //case it's 64 bit
    9. return location[2];
    10. #endif
    11. #ifdef Q_WS_X11 //case of linux -> this one gives me problems
    12. QDir dir();
    13. if (dir.exists(location[3]))
    14. return location[3];
    15. #endif
    16. return location[0];
    17. }
    To copy to clipboard, switch view to plain text mode 

    And got same error. Then I tried to switch #ifdef's. So linux one was first one and windows was second and expected to get error for dir.exists in windows #ifdef but I got same error for linux again and got little lost.

    In case that I changed Q_WS_X11 to anything else, like Q_MS_X11, program would compile but it wouldn't work on linux considering that Q_MS_X11 isn't right global.

    So... just wondering if someone could point me to right direction on how should I deal with it?
    And information on how to improve the function is welcomed too though don't be too harsh I'm just a beginner.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDir and problems with #ifdef OS

    Why not use the QFile static function exists() ?

    Qt Code:
    1. if (QFile::exists("foo"))
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to squidge for this useful post:

    ranko_6 (19th February 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDir and problems with #ifdef OS

    Qt Code:
    1. QDir dir();
    To copy to clipboard, switch view to plain text mode 
    This is declaration of function named "dir", that takes no parameters and returns QDir object
    I guess you mean
    Qt Code:
    1. QDir dir;
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to stampede for this useful post:

    ranko_6 (19th February 2011)

  6. #4
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDir and problems with #ifdef OS

    I got the error for both windows and linux define. Writing
    Qt Code:
    1. QDir dir;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QDir dir();
    To copy to clipboard, switch view to plain text mode 
    makes your method compile.

    stampedes answer just came in while I was still typing.

  7. The following user says thank you to ars for this useful post:

    ranko_6 (19th February 2011)

  8. #5
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDir and problems with #ifdef OS

    Thanks guys, think I'd still be trying some weird alternatives without you

  9. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDir and problems with #ifdef OS

    On Windows, you can try to use QSettings to read registry. Some programs puts the installation path in registry, so if you know how (and if) the program stores the path, you can read it. It will be more reliable than checking the "C:/ProgramFiles/*", because not everyone installs software on the system drive ( I remember having Windows XP installation even without the C:\ drive, I really don't know why, but disks were like I:\, J:\, K:\ ... )
    For example, to get install dir of ImgBurn you can write:
    Qt Code:
    1. QSettings settings("HKEY_CURRENT_USER", QSettings::NativeFormat);
    2. settings.beginGroup("Software");
    3. settings.beginGroup("ImgBurn");
    4. QString dir = settings.value("InstallDirectory","NULL").toString();
    To copy to clipboard, switch view to plain text mode 

    But this is only if you know what you are looking for.

  10. The following user says thank you to stampede for this useful post:

    ranko_6 (20th February 2011)

  11. #7
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDir and problems with #ifdef OS

    Thanks, thought about avoiding that alternative because I read on someone's post that they didn't have rights to read it, but in case that you described it would be pretty helpfull.

Similar Threads

  1. #ifdef QT?
    By ComServant in forum Newbie
    Replies: 2
    Last Post: 27th August 2010, 21:54
  2. QListView and QDir
    By Yayati.Ekbote in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2010, 11:45
  3. Help with QDir model
    By allstar in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2007, 15:31
  4. qdir and entrylist
    By mattia in forum Newbie
    Replies: 1
    Last Post: 28th November 2007, 10:13
  5. Is it me or QDir?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:06

Tags for this Thread

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.