Reinventing the wheel: qtQuick input widgets
-
Good afternoon,
I'm creating a form to let me edit the content of a database table. I've been looking at the QtQuick TextInput element but it lacks all but the most basic of features. Specifically I'd like:
- A property that indicates if the user has changed the content of the control
- A method to pass a type indicator to the control and have it automatically apply the correct validator/input mask. The QVariant type enumeration would be ideal.
It looks like there's no C++ class to inherit to add this functionality and it's very problematic to use QLineEdit in QML. Any suggestions? I'd much prefer a C++ solution.
Thanks
-
Hi,
[quote author="jsprenkle" date="1423081877"]
- A property that indicates if the user has changed the content of the control
[/quote]A QML object always emits a signal when a property is changed (see "Property Attributes":http://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#property-attributes)
The signal is <property>Changed, and the corresponding signal handler is on<Property>Changed. Example:
@
TextInput {
onTextChanged: console.log("Text changed to" + text)
}
@In additioin, TextInput provides the accepted() and editingFinished() signals. See http://doc.qt.io/qt-5/qml-qtquick-textinput.html
[quote author="jsprenkle" date="1423081877"]
- A method to pass a type indicator to the control and have it automatically apply the correct validator/input mask. The QVariant type enumeration would be ideal.
[/quote]IMHO, it would be much less effort to check the type in QML and pass the appropriate validator, as opposed to implementing the check in C++
However, if you really want to do it, you can subclass QQuickItem.
- A property that indicates if the user has changed the content of the control
-
Thanks for the reply.
I was surprised that the Qt4 widget version of the input control provides a property that indicates if the input was changed but the qml version does not. It would be used to determine if writing content back to the database was required after the form is dismissed.
If I subclass QQuickItem then I'll have to reimplement all the keyboard input handling code. I wanted to avoid reinventing the wheel. It's a bit disappointing. I did this in winforms, then vb6, then WPF, and here we are again.
Maybe I can start with the code from the widget and refactor it to inherit from QQuickItem.
Thanks again.
-
You're welcome.
[quote author="jsprenkle" date="1423144212"]I was surprised that the Qt4 widget version of the input control provides a property that indicates if the input was changed but the qml version does not. It would be used to determine if writing content back to the database was required after the form is dismissed.[/quote]You can implement it yourself in two lines:
@
TextInput {
property bool isModified: false
onTextChanged: isModified = true
}
@To unset the flag, just call isModified = false
[quote author="jsprenkle" date="1423144212"]Maybe I can start with the code from the widget and refactor it to inherit from QQuickItem.[/quote]I recommend implementing it in QML. It's much less effort.