[SOLVED] Passing text from QML textinput to C++ Qstring?
-
Hey all,
I'm trying to read text from a QML Textinput to a C++ QString. Maybe this has been done before in another forum post, but I have not been able to locate it. Most links to topics on this subject that I've found, have all pointed to "doc.trolltech.com" pages, which no longer exists. I've viewed "this page":http://qt-project.org/forums/viewthread/1779 but it really doesn't explain much. "This Page":http://wiki.forum.nokia.com/index.php/CS001625_-_Connecting_Qt_signal_to_QML_function was also a little help, but not too much... I'm still a little green with C++ and Qt's libraries.
Background - in case there's a simpler way I'm unsure of: I'm using 3 text inputs for a UDP program, the remote IP, and both port numbers. If there's a simpler way to acquire these, I'm all ears. Otherwise, the the program requires the user to specify a remote and host port, and remote IP address. That data needs to be bound to a qstring sot hat when the packet goes out, it has all the correct information.
I'm assuming that I'll need to bind the textinput to to something, so following the example given under the parent rectangle:
@ TextInput {
id: textInput1
x: 8
y: 88
width: 144
height: 20
text: lineEdit.text
font.pixelSize: 12
}
Binding{
target: lineEdit
property: "text"
value: input.text
}
@From this point, I'm not certain how to handle or connect this particular setting to a QString on the C++ side.
That much being said, would it be better to create a signal in QML and call a slotted function on the C++ side? Such as this:
in QML:
@ Rectangle {
id: main
signal dataInput;
anchors.fill: parent;TextField { id: dataText objectName: "inputText" x: 8 y: 88 width: 144 height: 20 font.pixelSize: 12 placeholderText: qsTr("Enter IP Address:") editingFinished: main.datainput() }
}@
C++ Code:
@QQuickView* view = new QQuickView(QUrl("main.qml"));
QQuickItem *textin = view->rootObject();
QObject::connect(textin, SIGNAL datainput()), myClass, SLOT(read_text()));@From this point again, I'm not sure how to read the text from the textbox...
Would this work? looks a little too easy to be correct:
@QString input;
QQuickItem* object = myDialog->rootObject();
QObject textinput = object->findChild<QObject>("inputText");
if (inputText)
{
textinput->convert.toString(input);
}
@
Not sure if this is correct - still quite green with C++ - Can anyone shed some light?Thanks!!
-
Ok all, I figured it out.
The text in the QML textinput is of type QVariant. So in order to access it, you have to first designate an objectName for the TextInput in your QML file:
@TextField {
id: textField1
objectName:"inputText"
x: 8
y: 120
placeholderText: qsTr("Text")
}@Then, locate it as such:
@
QQuickView* myObject;
QQiuckItem* object = myObject->rootObject();
QObject textin = object->findChild<QObject>("inputText");
@Once the child object is located convert it to QString:
@
QString input = textin->property("text").toString();
@This will put the text from the TextInput, or TextArea, etc into a string of your designation as shown. Although I haven't tried it, I'm going to assume that if I put a number in the text field, it will convert to integer form using toInt() instead of toString().