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. [solved] Accessing QML Component properties
Forum Updated to NodeBB v4.3 + New Features

[solved] Accessing QML Component properties

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 7.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.
  • K Offline
    K Offline
    kevinsharp
    wrote on last edited by
    #1

    I think there's something about scope in QML that I don't understand: when I embed a QML component in a view, how can I "see" properties of elements of the component?

    For example, the Qt SDK includes the search box example where three SearchBox.qml components are used in main.qml. Each SearchBox component includes a TextInput element. I want to use TextInput.text in some JavaScript that I'll execute from within main.qml. I can't figure out how to access the element or its properties. Here's the full setup:

    SearchBox.qml includes:

    @TextInput {
    id: textInput
    anchors { left: parent.left; leftMargin: 8; right: clear.left; rightMargin: 8; verticalCenter: parent.verticalCenter }
    focus: true
    selectByMouse: true
    }@

    and main.qml includes:

    @SearchBox { id: search1; KeyNavigation.tab: search2; KeyNavigation.backtab: search3; focus: true }
    SearchBox { id: search2; KeyNavigation.tab: search3; KeyNavigation.backtab: search1 }
    SearchBox { id: search3; KeyNavigation.tab: search1; KeyNavigation.backtab: search2 }
    @

    but when I try (in main.qml) something simple like:

    @Text {
    id: text1
    x: 56
    y: 78
    width: 80
    height: 20
    text: "contents of search box 1" + search1.textInput.text
    font.pixelSize: 12
    }
    @

    in my main.qml file I get an undefined error. Qt Creator doesn't offer any code completion options (for search1) that seem to indicate how to do this. I tried manually forcing search1.textInput (undefined), search1.textInput.text (my initial guess, still undefined) search1.text (undefined). I even tried search1.data and search1.data.text (gives me object placeholder and undefined respectively) because code completion told me search1.data at least was a valid option.

    How do I get at the contents of the search box from my main.qml?

    -- kevin

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcrochik
      wrote on last edited by
      #2

      The following main.qml works w/o issues using your SearchBox.qml:

      @Rectangle {

      width: 200
      height: 200
      Text {
          x: 66
          y: 93
          text: search1.text
      }
      
      Rectangle {
          x: 10
          y: 10
          color: "red";
          width: 100
          height: search1.height
          
          SearchBox {
              id: search1;
          }
      }
      

      }@

      Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kevinsharp
        wrote on last edited by
        #3

        tks. I've got something working now. Turns out there's something about the way the rest of the SearchBox component was built that's hiding the text from main.qml. When I use your code with the component that ships with the SDK, I still get an undefined error. But since it worked for you I stripped down the SearchBox component to just the TextInput element and it works. Not pretty yet, but it works. I can start adding back elements till I see what breaks it.

        Thanks.

        -- kevin

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcrochik
          wrote on last edited by
          #4

          I used the version of SearchBox that you posted:

          @TextInput {
          id: textInput
          anchors { left: parent.left; leftMargin: 8; right: clear.left; rightMargin: 8; verticalCenter: parent.verticalCenter }
          focus: true
          selectByMouse: true
          }@

          Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

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

            also it's better to write like @TextInput {@ instead of @TextInput
            {@

            sometimes it wont display properties for that object if you write like second one.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fcrochik
              wrote on last edited by
              #6

              [quote author="mohsen" date="1295785184"]also it's better to write like @TextInput {@ instead of @TextInput
              {@

              sometimes it wont display properties for that object if you write like second one.[/quote]

              If this is the case I would have to suggest that it is a bug and should be reported.

              Certified Specialist & Qt Ambassador <a href="http://www.crochik.com">Maemo, Meego, Symbian, Playbook, RaspberryPi, Desktop... Qt everywhere!</a>

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbrasser
                wrote on last edited by
                #7

                [quote author="kevinSharp" date="1295718328"]I think there's something about scope in QML that I don't understand: when I embed a QML component in a view, how can I "see" properties of elements of the component? ...
                How do I get at the contents of the search box from my main.qml?
                [/quote]

                The sub-elements within a component are considered private, so you'll need to define your own "public API" that exposes the properties you want to manipulate. http://doc.qt.nokia.com/4.7-snapshot/qml-extending-types.html gives a good overview of how you can go about this.

                Regards,
                Michael

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kevinsharp
                  wrote on last edited by
                  #8

                  [quote author="mbrasser" date="1295830345"]
                  The sub-elements within a component are considered private, so you'll need to define your own "public API" that exposes the properties you want to manipulate. http://doc.qt.nokia.com/4.7-snapshot/qml-extending-types.html gives a good overview of how you can go about this.[/quote]

                  Perfect. I knew I had seen documentation about this but I couldn't find it. This explains everything.

                  -- kevin

                  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