Custom TextField existing required property not initialized, but it is
-
wrote 23 days ago last edited by
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 thatTextField
inherits fromTextInput
inherits fromItem
inherits fromQtObject
.
The fieldimplicitWidth
is present inItem
and can be easily found in the list of all members.Things that I've tried:
- removed the
required
line -> it works - removed the
required
line and put theimplicitWidth
in theTextField
-> it works - created my own required property (named
length
for example) and assignedimplicitWith
to it -> it works (which is a valid workaround, but not convenient) - changing the type of
TextField
toComboBox
-> it works - changing the type of
TextField
toTextArea
-> 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 thatComboBox
inherits 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 thatTextField
does 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
TextField
withTextInput
in my example, you will get the errorInvalid property assignment: "implicitWidth" is a read-only property
, which is strange becauseTextInput
still inherits fromItem
and because remember that I was able to assign it without the required line with theTextField
type.Thanks for your time, have a nice day !
- removed the
-
wrote 23 days ago last edited by
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. -
wrote 23 days ago last edited by
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 aComboBox
and with aTextField
.The
ComboBox
can 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 ?
1/3