Custom TextField existing required property not initialized, but it is
-
Hi all,
I'm working on a QtQuick application using Qt version 6.8.2.
I've created a custom QML type definition of a TextField QML type (cf doc).
I've set several required existing properties (cf doc), which all work except forimplicitWidth.I've got the following error message:
MyText.qml:4:1: Required property implicitWidth was not initialized.
Here is my very simple code:// MyText.qml import QtQuick import QtQuick.Controls TextField { required clip required implicitWidth required opacity Text { text: qsTr("my square") } }Which can be called like this:
// Main.qml import QtQuick Window { MyText { clip: true implicitWidth: 520.0 opacity: 0.8 } }I've started digging to understand this behavior, but the more in dig, the less I understand.
In the doc, we can find thatTextFieldinherits fromTextInputinherits fromIteminherits fromQtObject.
The fieldimplicitWidthis present inItemand can be easily found in the list of all members.Things that I've tried:
- removed the
requiredline -> it works - removed the
requiredline and put theimplicitWidthin theTextField-> it works - created my own required property (named
lengthfor example) and assignedimplicitWithto it -> it works (which is a valid workaround, but not convenient) - changing the type of
TextFieldtoComboBox-> it works - changing the type of
TextFieldtoTextArea-> it does not work
I've chosen ComboBox and TextArea because they are both part of the Input Controls type.
The things that I've noticed is thatComboBoxinherits fromControl, which is not the case ofTextArea, norTextField(if we refer to the doc).
But, when I've looked at the code (here), it seems thatTextFielddoes indeed inherit fromControl. So maybe this is not the reason.Any explanation would be welcome !
PS: I've found another strange thing during my tests, if you replace
TextFieldwithTextInputin my example, you will get the errorInvalid property assignment: "implicitWidth" is a read-only property, which is strange becauseTextInputstill inherits fromItemand because remember that I was able to assign it without the required line with theTextFieldtype.Thanks for your time, have a nice day !
- removed the
-
implicitWidth exists on Item and is read-only (computed internally based on content/layout hints).
You cannot set implicitWidth manually on TextInput because it’s read-only there.
TextField inherits from TextInput (even though it’s a Control, it delegates its implicit size management to its internal contentItem, which is a TextInput).
The moment you mark implicitWidth as required, it expects you to initialize it explicitly from outside. But:
Since implicitWidth is not a regular property you can set (it's internally computed), QML complains that it wasn’t initialized.
// MyText.qml import QtQuick import QtQuick.Controls TextField { required clip required opacity required property real widthHint implicitWidth: widthHint Text { text: qsTr("my square") } }Window { MyText { clip: true opacity: 0.8 widthHint: 520.0 } }Never declare implicitWidth or any read-only, computed property as required.
Use a proxy property (like widthHint) or bind to width or implicitWidth internally without making them required. -
Thanks, but:
@Rangantha-B-V said in Custom TextField existing required property not initialized, but it is:
implicitWidth exists on Item and is read-only (computed internally based on content/layout hints).
I disagree. The doc does not write that the property is read-only. It says that "however some items have an inherent implicit size which cannot be overridden, for example, Image and Text", which I assume is the case of the
TextInput, but is not really mentioned precisely in the doc...
As a counter-example to read-only, you can set an implicitWidth with aComboBoxand with aTextField.The
ComboBoxcan have a required on implicitWidth without any problems, which is not the case ofTextField, why that ?@Rangantha-B-V said in Custom TextField existing required property not initialized, but it is:
bind to width or implicitWidth internally
So it's read-only from the outside but editable within the object ? In this case, why could I set it from the outside by simply removing the required line ?