0

I want to display the recently entered text to the label. Here's the code for the qml:

 Container {
                        horizontalAlignment: HorizontalAlignment.Center
                        verticalAlignment: VerticalAlignment.Top
                        topPadding: 100
                        leftPadding: 50
                        rightPadding: leftPadding

                        /*TextArea {
                            id: taComment
                            preferredHeight: 270
                            editable: quoteBubble.editMode
                            enabled: enableSave
                            input.flags: TextInputFlag.SpellCheckOff
                        }*/
                           Label {
                               verticalAlignment: VerticalAlignment.Top
                               horizontalAlignment: HorizontalAlignment.Center
                               text: cppObj.desc
                           }
                    } 

                    Container {
                        horizontalAlignment: HorizontalAlignment.Center
                        verticalAlignment: VerticalAlignment.Bottom
                        leftPadding: 50
                        rightPadding: leftPadding
                        bottomPadding: 40

                        TextField {
                            id: tfComment
                            hintText: qsTr("add comment")
                            inputMode: TextFieldInputMode.Text
                            input {
                                submitKey: SubmitKey.Submit
                                onSubmitted: {
                                    cppObj.onCommentSubmitClicked(tfComment.text, "");
                                }
                            }
                        }
                    }
                }

So when the user enters a phrase from the first textfield, I want that phrase to display in the label below it. More like a text messaging. How do I do that? And after displaying the entered text from textfield to label I want to save the label's text, so when I enter new comment it'll be saved to other label

kev
  • 155
  • 1
  • 11

4 Answers4

2

Add id to Label like following

 Label {
    id: label
    ....
 }

Then on TextField on submit handler, you can get text from textField and set it to label, like below

 onSubmitted: {
    label.text = tfComment.text;
 }
Kunal
  • 3,475
  • 21
  • 14
  • Hi, thanks for your reply and it works flawlessly. Edited my question though – kev Jul 24 '13 at 09:49
  • Do you mean, you want to create another label and pass current label's text to new one and display new entered text to current one ? – Kunal Jul 24 '13 at 16:27
  • Yes exactly. How do i do that? – kev Jul 25 '13 at 02:37
  • Assigning `tfComment.text` to `label.text` each time `onSubmitted` is certainly not the cleanest way to do (you can bind both properties, it will be done automatically, see my answer if you want to know how to do it). Regarding your second question, it's as simple as `label1.text = label2.text;` in the right slot. – Marc Plano-Lesay Jul 25 '13 at 11:41
  • 1
    in that case its best if you create list view and add data to list model, when you submit data from text field. So create one ListView and one TextField. On TextField submit event, add data to ListView's model, that will display data on ListView. – Kunal Jul 25 '13 at 17:48
1

This is all related to Qt signals/slots and properties mechanism. Your items, TextField and Label, have properties. A specific property they share in common is text (note that the name is the same, but what really matters is the type - two properties with different names could be binded too). You can bind a property to another one:

Label {
    text: myTextField.text
}
TextField {
    id: myTextField
}

Now, whenever the TextField's text changes, the Label's one is updated accordingly. Note that the TextField's text property is updated only when it looses its focus or when it's submitted.

This is the simple way to do what you need. There's a more generic way to do it. Qt's property system is based on signals/slots mechanism. A signal is something which a QObject can send, to notify other QObject children. Slots are a specific kind of methods, which can be connected to signals. Then, if a signal A is connected to a slot B, whenever the signal A will be emitted, the slot B will be called.

The TextField object has an input child, which has an submitted signal. As you can guess, this signal is emitted whenever the user submits the TextField. Eh, that's every time you want to change your Label's text property! Let's do this in a slot:

Label {
    id: myLabel
}
TextField {
    id: myTextField
    input {
        onSubmitted: {
            myLabel.text = myTextField.text;
        }
    }
}

Here, the onSubmitted function is a slot, which will be called automatically by Qt whenever the submit signal is emitted. You can of course do whatever you want in this slot: update properties, on this object or other ones, invoke a C++ method, … you're free.

You'll find a complete reference about signals and slots here, and about properties here (couldn't find this one on BlackBerry's documentation, but it's exactly what's used by BlackBerry).

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
0

Give your label an id (for example id : myLabel), then in your onSubmitted slot assign the text of the TextField to that label like this:

onSubmit :{
    myLabel.text = tfComment.text
}
unexplored
  • 1,414
  • 3
  • 18
  • 37
-1

First you must set objectNames in the relevant places in the QML.

...
Label {
    objectName: "label"
    ...
}
...
TextField {
    objectName: "textField"
    ...
}

Of course I'm not suggesting that these are good names.

Once this is done, from where you use the QML in C++ i.e.:

QmlDocument *qml = QmlDocument::create("asset///qml/YourQml.qml");
Container *container = qml->createRootObject<bb::cascades::Container>();

You can then find the root object's (just assumed container here) children and manipulate them by name.

TextField *textField = container->findChild<TextField*>("textField");
Label *label = container->findChild<Label*>("label");
...
label->setText(textField->text()); //take the text from textField and display in label
Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
  • It would surely work, but there are much easier ways to do it. – Marc Plano-Lesay Jul 23 '13 at 01:38
  • Perhaps. But what is necessary or not is circumstantial. I only attempted to provide one approach. If for example the text needs to be massaged in some fashion before it is displayed, then going through C++ is the best way to go. I certainly don't think I deserve a downvote for that – Konrad Lindenbach Jul 23 '13 at 02:55