How to add/edit text from C++ in QML TextInput?
-
Hi,
How can I control the TextInput QML control from c++? I don' have a touch screen, only hardware buttons so I have to control the TextInput from C++.
I want to add / edit text, how can this be archieved? I don't see any TextInput (QML) methods that I can use from C++ to do this.
Thanks!
-
You can contol TextInput over these methods:
select
selectAll
selectWordand properties:
cursorPosition
text"QML functions can be called from C++ and vice-versa.":http://doc.trolltech.com/4.7-snapshot/qtbinding.html#calling-functions
-
I think "this":http://doc.trolltech.com/4.7-snapshot/qtbinding.html#modifying-properties may help you. The text is just another property of the object.
Edit:
One way to get a reference to the right object (created in QML) from C++ would be to give the object an object name in QML, and then search for that name in C++. Something like (untested!):qml:
@
TextInput {
id: textInput
objectName: textInput
//...
}
@c++
@
QDeclarativeContext* context = m_declarativeView->rootContext();
QObject* textInput = context->findChild<QObject*>("textInput");
if (textInput) { //never simply dereference without checking if the output is valid
textInput->setProperty("text", QString("My very fancy string!"));
}
@Of course, if you need to modify the text (or other properties) more than once, then you only need to search for the object once, after which you can manipulate them from code later.
-
I know I can connect to the TextInput QML element from C++ and access properties. But I also want the functionality of the TextInput QML element in C++.
So when the cursor is set in the middle of the TextInput how can i easily add some text in the middle without having to looking at the current position, copy entered text in a QString, set the text property of TextInput and set the cursor to the new position?
I guess when using Qt widgets this wouldn't be a problem?
-
Hmm it doesn't feel good to use a private header file to get this to work.. I think i'll make my own class and connect it to the text and cursorPosition properties of the TextInput QML element...
Or I can try to find the object like andre told me and then send key-events to it...
@
TextInput {
id: textInput
objectName: textInput
//...
}
@Then:
@
QDeclarativeContext* context = m_declarativeView->rootContext();
QObject* textInput = context->findChild<QObject*>("textInput");
QApplication::postEvent(textInput, keyevent);
@