Important: Please read the Qt Code of Conduct -

Setting width based on text

  • This is an extension of my last post. I am working on a custom TextInput control. I started with the "search box": example and have been modifying from that point.

    The desired effect is a control I saw in the Mac Address box. When you go to create a new contact, you see the grayed text of what you are suppose to enter: [First] [Last] where the brackets denote the TextInput control. When the text William is entered into the [First] TextInput control, it expands to fit the whole word.

    My custom control, like the "search box": example, is a FocusScope Text that contains a Text element that shows the message string, aka First or Last. There is a BorderImage that is visible when the control has focus and a TextInput.

    What I would like to do is this:

    As long as the TextInput text is empty, make the TextInput and BorderImage the same size as the Text.text.

    Once the TextInput has text, set both its width and the BorderImage to be as wide as the TextInput.text.

    Can this be done? If so, now?


  • Hi,

    You should be able to set the width of a TextInput back to its "implicit width" (the width of the text it contains), by assigning undefined to its width property, either in script:

    @textInput.width = undefined@

    or in a PropertyChanges

    @ PropertyChanges {
    target: textInput
    width: undefined


Log in to reply