Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Variables in QML?

Variables in QML?

Scheduled Pinned Locked Moved C++ Gurus
9 Posts 6 Posters 26.2k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    eirnanG
    wrote on last edited by
    #1

    Can i declare a variable in QML??

    ....because I want to create a qml that can call other qml with a condition (like if else)
    or i must do it in cpp?

    thanks! :D

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bergo.torino
      wrote on last edited by
      #2

      No, you cannot. You have to export your variable from C++ layer down to the QML by using QDeclarativeContext::setContextProperty().

      It's compiling - mostly I'm slacking off ;)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        baysmith
        wrote on last edited by
        #3

        Use properties. See http://developer.qt.nokia.com/doc/qt-4.7/propertybinding.html

        Nokia Certified Qt Specialist.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          eirnanG
          wrote on last edited by
          #4

          i think i should start making my cpp..

          @bergo thanks! i will study that code. :D
          @bradley thanks for the link. =)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mikhas
            wrote on last edited by
            #5

            You can also create a component and then use an instance of it:

            @
            SomeThing {
            id: instanceOfSomeThing
            }

            ...

            instanceOfSomeThing.someProperty = someValue
            @

            someProperty can also be used in conditions.

            [EDIT: code formatting, Volker]

            1 Reply Last reply
            0
            • E Offline
              E Offline
              eirnanG
              wrote on last edited by
              #6

              @mikhas thanks! that code helps a lot :)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AlterX
                wrote on last edited by
                #7

                You can also create a .js (shared o stateful) and use it.

                Qt Ambassador
                Real-time cooperative teams: http://www.softairrealfight.net
                Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                https://codereview.qt-project.org/...

                1 Reply Last reply
                0
                • AlicemirrorA Offline
                  AlicemirrorA Offline
                  Alicemirror
                  wrote on last edited by
                  #8

                  AlterX,

                  I think this is the best way to solve the problem. I am developing stuff in this direction and what is really helpful is js associated with the usage of the loader element.

                  You can create a QML document of the type
                  @
                  import "MyScript.js" as Script

                  Item {
                  id: myItem
                  ...
                  Loader {
                  id: myLoader
                  [... set loader properties ...]
                  source: Script.getDocumentToLoad()
                  }
                  }
                  @
                  Then in the .js script you create the function like
                  @
                  function getDocumentToLoad() {

                  if(myTimeOfDay() > 12) {
                      return "AfternoonWidget.qml"
                  else
                      return "MorningWidget.qml"
                  

                  }
                  @

                  Hope this can help you.

                  Enrico Miglino (aka Alicemirror)
                  Balearic Dynamics
                  Islas Baleares, Ibiza (Spain)
                  www.balearicdynamics.com

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    AlterX
                    wrote on last edited by
                    #9

                    @Alicemirror: Yes it is... ;-)

                    Qt Ambassador
                    Real-time cooperative teams: http://www.softairrealfight.net
                    Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

                    https://codereview.qt-project.org/...

                    1 Reply Last reply
                    0

                    • Login

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