PDA

View Full Version : QDir and problems with #ifdef OS



ranko_6
19th February 2011, 18:27
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.


QString Program::ProgramFound()
{
QDir dir();
QString location[4] = {"No", "C:/Program Files/Program", "C:/Program Files(x86)/Program", "~/.wine/drive_c/Program Files/Program"};
#ifdef Q_MS_WIN //case of windows
if (dir.exists(location[1])) //case it's 32 bit
return location[1];
if (dir.exists(location[2])) //case it's 64 bit
return location[2];
#endif
#ifdef Q_WS_X11 //case of linux -> this one gives me problems
if (dir.exists(location[3]))
return location[3];
#endif
return location[0];
}

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:


QString Program::ProgramFound()
{
QString location[4] = {"No", "C:/Program Files/Program", "C:/Program Files(x86)/Program", "~/.wine/drive_c/Program Files/Program"};
#ifdef Q_MS_WIN //case of windows
QDir dir();
if (dir.exists(location[1])) //case it's 32 bit
return location[1];
if (dir.exists(location[2])) //case it's 64 bit
return location[2];
#endif
#ifdef Q_WS_X11 //case of linux -> this one gives me problems
QDir dir();
if (dir.exists(location[3]))
return location[3];
#endif
return location[0];
}

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?
:p And information on how to improve the function is welcomed too though don't be too harsh :) I'm just a beginner.

squidge
19th February 2011, 22:40
Why not use the QFile static function exists() ?



if (QFile::exists("foo"))

stampede
19th February 2011, 22:58
QDir dir();
This is declaration of function named "dir", that takes no parameters and returns QDir object ;)
I guess you mean

QDir dir;

ars
19th February 2011, 23:06
I got the error for both windows and linux define. Writing

QDir dir;
instead of

QDir dir();
makes your method compile.

stampedes answer just came in while I was still typing.

ranko_6
19th February 2011, 23:42
Thanks guys, think I'd still be trying some weird alternatives without you :)

stampede
20th February 2011, 10:08
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:


QSettings settings("HKEY_CURRENT_USER", QSettings::NativeFormat);
settings.beginGroup("Software");
settings.beginGroup("ImgBurn");
QString dir = settings.value("InstallDirectory","NULL").toString();


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

ranko_6
20th February 2011, 12:28
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.