May 12, 2011

Hedge Hedge
Lab Rat
191 posts

Platform specific part of QML-file?

 

I want a qml-object to only show up if my application is compiled for mac os. How do I do that?

10 replies

May 12, 2011

Andre Andre
Area 51 Engineer
6076 posts

As far as I know, there is no API for that (yet). You can create a QObject in C++ that exposes the platform and insert that in your QML context.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 12, 2011

Hedge Hedge
Lab Rat
191 posts

Ew.. I expected that. The desktop-components are too fresh yet.

Can I find an example for the method you describe anywhere?

May 12, 2011

Andre Andre
Area 51 Engineer
6076 posts

Not that I know of, but it would be quite simple I think:

  1. //header
  2. class PlatformDetails:public QObject
  3. {
  4.     Q_OBJECT
  5.     Q_PROPERTY (bool isMac READ isMac)
  6.     public:
  7.     PlatformDetails(QObject*parent =0);
  8.     bool isMac();
  9. }
  10.  
  11. //implementation:
  12. PlatformDetails::PlatformDetails(QObject* parent):
  13.    QObject(parent)
  14. {
  15. }
  16.  
  17. bool PlatformDetails::isMac()
  18. {
  19. #ifdef Q_OS_MAC
  20.    return true;
  21. #elif
  22.    return false;
  23. #endif
  24. }

And then you simply create an instance an insert it into your QML context. Of course, you can make the class a bit more useful by returning an enum that can represent all the platforms instead of a simple boolean for mac, but you get the idea.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 4, 2011

artem.marchenko artem.marche..
Lab Rat
61 posts

Could there be some compile-time switch?

After all the QML code is wrapped into a c++ application and at the moment of compilation you do know the platform. Then a #define could include different Settings.qml for each platform. That is what I am thinking about for my own project (need some fine-tuning for Symbian, MeeGo, Android).

It would be cool to see a complete example from somebody though.

June 5, 2011

mario mario
Lab Rat
240 posts

Maybe you can do that in the .pro file. At least you can check for Win, Mac and Unix.

June 5, 2011

artem.marchenko artem.marche..
Lab Rat
61 posts

I had a similar challenge to have some variation just for Maemo (and not for Symbian). My project is pure QML, c++ part is just a thin wrapper generated by Qt Creator wizard and I wanted the platform dependence be isolated to as small piece of c++ as possible.

What I ended up with was using C macros in main.cpp to define a qml context-wide property the following way:

  1. #if defined(Q_OS_SYMBIAN)
  2.     int platformId = 0;
  3. #elif defined(Q_WS_MAEMO_5)
  4.     int platformId = 1;
  5. #elif defined(QT_SIMULATOR)
  6.     int platformId = 2;
  7. #else
  8.     // desktop probably
  9.     int platformId = 3;
  10. #endif
  11.  
  12.     viewer.rootContext()->setContextProperty("platform",  platformId);
  13.     viewer.setMainQmlFile(QLatin1String("qml/MultiPlatformUi/MultiPlatformUi.qml"));
  14.     viewer.showExpanded();
  15.  
  16.     return app.exec();

Then in your qml you’ve got a platform property that can be used when doing property binding or inside a function the following way (my code returns a different set of settings for the different platform):

  1. function resolvePlatform() {
  2.         switch(platform) {
  3.         case 0:
  4.             return setSymbian
  5.         case 1:
  6.             return setMaemo
  7.         case 2:
  8.             return setSimulator
  9.         default:
  10.             return setDesktop
  11.         }
  12.     }

Certainly this run-time platform dependency switch still means that you need to include all your files to the project regardless of whether they make sense for a platform – I didn’t find a way to use .pro syntax for replacing e.g. Settings.qml at build time.

That also means that unless you are ready to mess with Loader your qmls should compile on all the platforms you are interested in. For example, if you want to target Maemo5 as well, you need to use “import Qt 4.7”, not “import QtQuick 1.0”.

P.S.
The code above is proof of concept for now. In your real app, you will certainly use proper constants, not just numbers.

June 9, 2011

moo1 moo1
Lab Rat
33 posts

@artem I wonder if it works but you may try DEPLOYMENTFOLDERS or DEPLOYMENT with platform scope values to include qml or whatever files that is only needed for the target platform.

June 10, 2011

artem.marchenko artem.marche..
Lab Rat
61 posts

I tried playing with DEPLOYMENTFOLDERS and failed. QML files are gathered using poorly documented combo of folder_01.source, folder_01.target and DEPLOYMENTFOLDERS

Whatever I was doing I failed to put qmls from two different source folders into the same path inside a binary – they always tend to have a path specific component. Further investigation showed that this logic comes from generated qmlapplicationviewer.pri, but messing with it was too much for me.

June 10, 2011

moo1 moo1
Lab Rat
33 posts

hmm, not sure what’s not exactly working for you but platform directive + DEPLOYMENTFOLDERS seems to work for me.

something like…

  1. symbian{
  2. f3.source = ./symbian/plat
  3. f3.target = qml
  4. DEPLOYMENTFOLDERS += f3
  5. }
  6. win32{
  7. f3.source = ./win/plat
  8. f3.target = qml
  9. DEPLOYMENTFOLDERS += f3
  10. }

You’ll have qml/plat/ folder in target with files depending on the platform.

Maybe DEPLOYMENT is more flexible.

June 12, 2011

artem.marchenko artem.marche..
Lab Rat
61 posts

Hmm, I tried the similar code and it was resulting in two subfolders in the binary. If I remember correctly, I was getting something like: – qml/symbian/plat – qml/win/plat – qml/common (I had some common files as well)

That may be fine depending on what you want, but I wanted to put the files from both /win/plat and /symbian/plat to the same directory so that I could just replace Settings.qml and maybe some layout files.

Maybe this behavior is platform-specific. I tried on Mac OS X

 
  ‹‹ [Solved] Stripping double quotes from string      [SOLVED]QtMobility in QML ››

You must log in to post a reply. Not a member yet? Register here!