Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Custom TextField existing required property not initialized, but it is
QtWS25 Last Chance

Custom TextField existing required property not initialized, but it is

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlqtquickqml typecontrols
3 Posts 2 Posters 93 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RomainW
    wrote 23 days ago last edited by
    #1

    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 for implicitWidth.

    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 that TextField inherits from TextInput inherits from Item inherits from QtObject.
    The field implicitWidth is present in Item 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 the implicitWidth in the TextField -> it works
    • created my own required property (named length for example) and assigned implicitWith to it -> it works (which is a valid workaround, but not convenient)
    • changing the type of TextField to ComboBox -> it works
    • changing the type of TextField to TextArea -> 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 that ComboBox inherits from Control, which is not the case of TextArea, nor TextField (if we refer to the doc).
    But, when I've looked at the code (here), it seems that TextField does indeed inherit from Control. 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 with TextInput in my example, you will get the error Invalid property assignment: "implicitWidth" is a read-only property, which is strange because TextInput still inherits from Item and because remember that I was able to assign it without the required line with the TextField type.

    Thanks for your time, have a nice day !

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rangantha B V
      wrote 23 days ago last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RomainW
        wrote 23 days ago last edited by
        #3

        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 a ComboBox and with a TextField.

        The ComboBox can have a required on implicitWidth without any problems, which is not the case of TextField, 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 Reply Last reply
        0

        1/3

        17 Apr 2025, 10:49

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved