×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Nokia Developer Expert
    Join Date
    Oct 2009
    Posts
    6

    How to retrieve vars from QML components

    I seem to be missing a basic construct here: how to I access a value created in a QML component from a QML file that embeds it? For example, the Qt SDK includes the search box example where the SearchBox.qml component is used in main.qml.

    SearchBox.qml includes:

    Code:
    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:
    Code:
         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 something simple like:

    Code:
    Text {
            id: text1
            x: 56
            y: 78
            width: 80
            height: 20
            text: "contents of search box 1" + search1.textInput
            font.pixelSize: 12
        }
    in my main.qml file I get an undefined error.

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

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Re: How to retrieve vars from QML components

    You don't need to use elements internal id from outside. Point directly to text property:

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

  3. #3
    Nokia Developer Expert
    Join Date
    Oct 2009
    Posts
    6

    Re: How to retrieve vars from QML components

    Quote Originally Posted by tliljend View Post
    You don't need to use elements internal id from outside. Point directly to text property:
    @tliljend: thanks for the reply, but I'm still getting an undefined error. There must be something about scoping in QML that I'm not understanding.

    When I type "search1." Qt Creator presents me with a code completion list that seems to apply to the visual container, but not the data content. Under the hope that it was just a code completion bug, I tried manually forcing search1.textInput (undefined), search1.textInput.text (my initial guess, still undefined) search1.text (ur suggestion, still 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.
    I need to dig into the documentation and get a better understanding of scope.

  4. #4
    Nokia Developer Expert
    Join Date
    Oct 2009
    Posts
    6

    Re: How to retrieve vars from QML components

    Quote Originally Posted by KevinSharp View Post

    When I type "search1." Qt Creator presents me with a code completion list that seems to apply to the visual container, but not the data content.
    That's by design. The SearchBox.qml component that came with the SDK has a FocusScope as the root element, with the TextInput element as a sub element. Subelements are private, but you can explicitly expose properties. See http://doc.qt.nokia.com/4.7-snapshot...ing-types.html

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    Thumbs up Re: How to retrieve vars from QML components

    I think the problem is that SearchBox.qml itself does not have a text variable, one of its children (the TextInput) does. Try to add:
    Code:
    property string text: textInput.text
    to the main parent of TextInput.

    Alternatively you can add a simple getText() function to the parent:
    Code:
    function getText(){
       return textInput.text;
    }
    And then just refer to that in the Text element:

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

Similar Threads

  1. [Qt QML] Questions regarding QML Performance
    By SeanZhang in forum [Archived] Qt Quick
    Replies: 1
    Last Post: 2011-05-05, 13:07
  2. Replies: 0
    Last Post: 2010-08-31, 04:05
  3. Carbide failing on compile - stuck on env vars
    By jgitter in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 4
    Last Post: 2008-10-10, 00:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×