Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to access to a qml object from javascript

How to access to a qml object from javascript

Scheduled Pinned Locked Moved Language Bindings
4 Posts 3 Posters 2.6k 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.
  • C Offline
    C Offline
    chrissou
    wrote on 21 Aug 2014, 08:34 last edited by
    #1

    Hi.
    Newbie with Qt, I just started using it.
    I have a very simple soft

    The question is how to access "text" of my dynamically created CustomButton object.
    It works using
    @
    property string text
    @

    and

    @
    button.text = "toto"
    @

    But is it the right way to do it ?
    Can I avoid the property and use something like

    @
    button.button.text = "toto"
    @

    to acces directly to the text of my button ?

    Thanks

    Forum.qml
    @import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Window 2.0

    import "Forum.js" as Logic

    ApplicationWindow {
    width: 640
    height: 480

    Component.onCompleted:
    {
        Logic.init(this)
    }
    

    }@

    forum.js
    @
    function init(parent) {
    var button = Qt.createQmlObject('import QtQuick 2.2; CustomButton {width:100; height:100}', parent, "dynamicSnippet1");

    button.text = "toto"
    

    }
    @

    CustomButton.qml
    @
    import QtQuick 2.0
    import QtQuick.Controls 1.1

    Item {
    property string text

    Button {
        id:button
        width: 100
        height: 62
        x : 100
        y : 100
    }
    
    onTextChanged:
    { button.text = text }
    

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 21 Aug 2014, 09:09 last edited by
      #2

      You have just defined the text inside the item. From outside it is just tex from Item. If you want to the text of button, you need make the top text as alias for the button text

      e.g
      Item {
      property alias text : button.text

      }

      Now from outside you refer using button.text

      Hope this helps.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chrissou
        wrote on 21 Aug 2014, 09:12 last edited by
        #3

        Ok
        I didn't know the "alias" functionnality...

        Looks perfect...

        Thanks

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 21 Aug 2014, 09:16 last edited by
          #4

          Hi,

          Your first way is correct
          @
          button.text = "toto"
          @

          you can avoid the onTextChanged handler if you solely want to assign the text,
          @
          function init(parent) {
          var button = Qt.createQmlObject('import QtQuick 2.2; CustomButton {width:100; height:100}', parent, "dynamicSnippet1");

              button.mytext = "toto"
          

          }

          Item {
          property string mytext

          Button {
              id:button
              width: 100
              height: 62
              x : 100
              y : 100
              text: mytext
          }
          

          }
          @

          Notice above that mytext is binded to text property of Button.

          bq. Can I avoid the property and use something like
          button.button.text = "toto"
          to acces directly to the text of my button ?

          You can't access it using id.id, but another non recommended way to do this to get the exact child using children as,
          @
          button.children[0].text = "Sometext"
          @

          Since we can guess the exact child number by merely seeing the QML file we are able to do the above; but it is not recommended.

          157

          1 Reply Last reply
          0

          1/4

          21 Aug 2014, 08:34

          • Login

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