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. Simple javascript [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Simple javascript [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.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.
  • G Offline
    G Offline
    gleach31
    wrote on last edited by
    #1

    I'm new, so first let me apologise for asking what must seem like a very simple question.

    If I have a qml file like :

    Rectangle {
    width: 200
    height: 200
    color :"blue"
    Text {
    id : whatever
    text: "?"
    color : "white"
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter
    }
    }

    and then use it in javascript to create a component and when it's ready, create an object, I can reference the rectangle using the object returned. But how, in javascript, do I play with the values in the Text part, eg, the text string itself, text colour, etc.

    I can't find a simple example in the javascript documentation that does this - javascript within the documentation appears to be a second class citizen. But most likely I'm missing something !

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      You can do it in 2 ways:

      1. Declare a property in Rectangle and then bind it to the text property of Text element
        @
        Rectangle {
        property string sampleText
        ...
        Text {
        id : whatever
        text: sampleText
        ...
        }
        }
        @

      And then when you create it, access that property with the returned object.
      @
      var obj = comp.createObject(parent)
      obj.sampleText = "SomeText"
      @

      1. Use the children property
        @
        obj.children[0].text = "SomeText"
        @

      If there are many such items you will need to iterate over them to find the exact one using objectName.

      157

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gleach31
        wrote on last edited by
        #3

        Thanks. Perfect answer !

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          You're Welcome :)
          Since you are new here, few things:

          1. No need to apologize. There are no dumb questions :)
          2. Please use code tags "@@" for posting the code part. This makes it easy for everyone to read.
          3. Please post the questions in relevant categories. It's not that you wont get an answer but that is what the categories are made for :)
          4. Append [solved] if your question is answered (and solved ofcourse) by editing the post title.

          Happy Coding..

          157

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gleach31
            wrote on last edited by
            #5

            Just to pick up on your point 3, in which category should I have posted ?. I felt that general was the right category.

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              Since the question is related to QtQuick/QML and Javascript, the proper place is Forums > Qt Development > Qt Quick.
              The post has been already moved here now.

              157

              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