[Qt-interest] Developing for multiple platforms (os)?
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Mon Feb 16 15:41:54 CET 2009
Andreas Pakulat wrote on Monday, February 16, 2009 11:00 AM:
> On 16.02.09 09:33:12, Knapp wrote:
>> Thanks everyone for all the great insights. The stuff about file path
>> formats is something I had forgotten about because I have been doing
>> so much python of late and they have a great lib to take care of
>> that problem.
>
> You can use Qt as well to get rid of that problem. QFile* and QDir
> all take native paths as well as "standard" paths using forward
> slashes.
That's true! I found the most fool-proof thing to do is to use /forward/slash/paths/like/this programmatically when you deal with the Qt API, and as soon as you actually need to present a path in the GUI (which then depends on which OS the user is working), you can use: QDir::toNativeSeparators (http://doc.trolltech.com/4.4/qdir.html#toNativeSeparators).
In analogy there is also a QDir::fromNativeSeparators method, but as Andreas pointed already out, the Qt API also accepts the native (Windows) file format. On Windows even this works:
QString dirPath = "\\\\server\\some\\shared\\directory\\"; // some user input in UNC notation
QDir networkDirectory = QDir(dirPath);
I haven't tried what
networkDirectory .absolutePath()
would return in this case, but according to the Qt docs ("a path that starts with "/" or with a drive specification ...") I would expect a mapped drive path, something like
g:/some/shared/directory/
where 'g' happens the drive-letter to which the UNC path above is mapped (another interesting question arises off course if the UNC path \\server\some\shared\directory\ is not yet mapped, but haven't tried that either in practice so far).
Regarding another issue - incorporating platform dependent code - I found this to be a best practice approach: Write an interface (header) and have the actual platform-dependent implementation in files called
UsbStuff.h // the header :)
UsbStuff.cpp // common implementation
UsbStuff_Windows.cpp // Windows implementation
UsbStuff_Unix.cpp // Unix/Linux
UsbStuff_MacOSX.cpp // Mac OSX etc.
and then only compile/link those files necessary for the specific platform. That's easy with e.g. qmake scopes
win32 {
sources += UsbStuff_Windows.cpp
}
This way you avoid excessive #ifdef Q_OS_WINDOWS stuff and it is instantly clear which source code contains platform-specific code :)
Cheers, Oliver
Cheers, Oliver
More information about the Qt-interest-old
mailing list