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?
-
Well, you can check if the QDeclarativeTextInput supports the API you need for your manipulation.
Some widgets may have enough external API to do this detailed manipulation using external API, but also not all of them.
-
I don't know if it is possible to send keyevents to QML app, just an idea...
-
That would be an idea ... But where do I find the header file of QDeclarativeTextInput? It's not in the Sdk?
-
Header is somewere in "QtSDK\QtSources\4.7.x\src\declarative\graphicsitems".
-
There only is a private header, but you can still inspect it. Name is qdeclarativetextinput_p.h in the folder mentioned by Alexkey78
-
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);
@ -
You don't have to use the private header at all. You can just use it see what properties are supported (and what slots and signals there are). You can invoke all of those without using the private header.