Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Platform specific part of QML-file?
QtWS25 Last Chance

Platform specific part of QML-file?

Scheduled Pinned Locked Moved QML and Qt Quick
15 Posts 8 Posters 25.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hedge
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hedge
        wrote on last edited by
        #3

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

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

        1 Reply Last reply
        1
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Not that I know of, but it would be quite simple I think:
          @
          //header
          class PlatformDetails:public QObject
          {
          Q_OBJECT
          Q_PROPERTY (bool isMac READ isMac)
          public:
          PlatformDetails(QObject*parent =0);
          bool isMac();
          }

          //implementation:
          PlatformDetails::PlatformDetails(QObject* parent):
          QObject(parent)
          {
          }

          bool PlatformDetails::isMac()
          {
          #ifdef Q_OS_MAC
          return true;
          #elif
          return false;
          #endif
          }
          @

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            artem.marchenko
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mario
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                artem.marchenko
                wrote on last edited by
                #7

                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:
                @#if defined(Q_OS_SYMBIAN)
                int platformId = 0;
                #elif defined(Q_WS_MAEMO_5)
                int platformId = 1;
                #elif defined(QT_SIMULATOR)
                int platformId = 2;
                #else
                // desktop probably
                int platformId = 3;
                #endif

                viewer.rootContext()->setContextProperty("platform",  platformId);
                viewer.setMainQmlFile(QLatin1String("qml/MultiPlatformUi/MultiPlatformUi.qml"));
                viewer.showExpanded();
                
                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):
                @function resolvePlatform() {
                switch(platform) {
                case 0:
                return setSymbian
                case 1:
                return setMaemo
                case 2:
                return setSimulator
                default:
                return setDesktop
                }
                }@

                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.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  moo1
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    artem.marchenko
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      moo1
                      wrote on last edited by
                      #10

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

                      something like...
                      @
                      symbian{
                      f3.source = ./symbian/plat
                      f3.target = qml
                      DEPLOYMENTFOLDERS += f3
                      }
                      win32{
                      f3.source = ./win/plat
                      f3.target = qml
                      DEPLOYMENTFOLDERS += f3
                      }
                      @

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

                      Maybe DEPLOYMENT is more flexible.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        artem.marchenko
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qt_sur
                          wrote on last edited by
                          #12

                          Just a quick update to the thread: It seems that now you can check the platform directly in your QML code:

                          @ if (Qt.platform.os === "linux" || Qt.platform.os === "windows" || Qt.platform.os === "osx" || Qt.platform.os === "unix") {
                          //..
                          }
                          @

                          This doesn't seem to be documented anywhere else though :S (so not sure if this can change, without notice).

                          Got the reference from:
                          http://qt-project.org/forums/viewthread/39956
                          http://qt-project.org/doc/qt-5/qtmultimedia-video-qmlvideofx-qml-qmlvideofx-main-qml.html

                          1 Reply Last reply
                          2
                          • jeremy_kJ Offline
                            jeremy_kJ Offline
                            jeremy_k
                            wrote on last edited by
                            #13

                            "QQmlFileSelector":http://qt-project.org/doc/qt-5/qqmlfileselector.html is intended to handle this situation.

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            1 Reply Last reply
                            0
                            • jeremy_kJ Offline
                              jeremy_kJ Offline
                              jeremy_k
                              wrote on last edited by
                              #14

                              [quote author="qt_sur" date="1411406074"]Just a quick update to the thread: It seems that now you can check the platform directly in your QML code:

                              @ if (Qt.platform.os === "linux" || Qt.platform.os === "windows" || Qt.platform.os === "osx" || Qt.platform.os === "unix") {
                              //..
                              }
                              @

                              This doesn't seem to be documented anywhere else though :S (so not sure if this can change, without notice).

                              [/quote]

                              That functionality is documented as part of the "QML global Qt object":http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#qmlglobalqtobject.

                              I wouldn't worry about it disappearing.

                              Asking a question about code? http://eel.is/iso-c++/testcase/

                              1 Reply Last reply
                              2
                              • ArtmeticA Offline
                                ArtmeticA Offline
                                Artmetic
                                wrote on last edited by
                                #15

                                My solution.
                                width: Qt.platform.os == "Android" ? Screen.width : 480
                                height: Qt.platform.os == "Android" ? Screen.height : 640

                                1 Reply Last reply
                                1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved