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. How can i set default TextField Border with current Style
Qt 6.11 is out! See what's new in the release blog

How can i set default TextField Border with current Style

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 64 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    PoirotMB
    wrote last edited by PoirotMB
    #1

    I have a pretty dynamic TextField which you can see in the picture. No visible border unless it's clicked then the placeholderText moves inside the border which i quite like. However since i want the border to be grey to match the background in case the TextField is greyed out i set the Background to disabledBackground. This works fine, however i can't return to default afterwards.

    I've tried the Componenet with an empty Background implemented as enabledBackground and I've tried setting the background to null which also didn't work.

    As you can see in the pictures the border is not visible anymore when i click the TextField. This happens only when the background was set to something before.

    Good:Screenshot 2026-05-11 132635.png

    Bad:394cacfd-a1b4-4cb7-98cf-3be1d1438986-image.png

               Component {
                    id: disabledBackground
                    Rectangle {
                        color: "transparent"
                        border.width: 1
                        border.color: "grey"
                        radius: 10
                    }
                }
    
                Component {
                    id: enabledBackground
                    Rectangle {
                    }
                }
    
                TextField {
                    id: textField
                    anchors {
                        fill: parent
                        leftMargin: 4
                        rightMargin: 4
                        topMargin: 4
                        bottomMargin: 4
                    }
    
                    verticalAlignment: Text.AlignVCenter
                    placeholderTextColor: "#4b4e5c"
                    color: "black"
                    font.pixelSize: 14
    
                    onEnabledChanged: {
                        if (!enabled) {
                            background = disabledBackground.createObject(this)
                        }
                        else{
                            background = enabledBackground.createObject(this)
                        }
                    }
    
                    placeholderText:
                        Array.isArray(root.placeholderText)
                        ? (index < root.placeholderText.length ? root.placeholderText[index] : "")
                        : root.placeholderText
    
                    onTextEdited: {
                        root.values[index] = text
                    }
    
                    enabled: root.fieldEnabled[index]
                }
    

    To cut my question short: How do i set the Background to the default based on the Material used. (not empty Rectangle, not null)

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PoirotMB
      wrote last edited by
      #2

      I have also tried taking the implementation directly from the TextField.qml file from the Material Style which results in this behaviour:

      As you can see it is always visible and doesnt "make space" for the placeholder Text.
      Screenshot 2026-05-11 134044.png

                  Component {
                      id: enabledBackground
                      MaterialTextContainer {
                          implicitWidth: 120
                          implicitHeight: control.Material.textFieldHeight
      
                          filled: control.Material.containerStyle === Material.Filled
                          fillColor: control.Material.textFieldFilledContainerColor
                          outlineColor: (enabled && control.hovered) ? control.Material.primaryTextColor : control.Material.hintTextColor
                          focusedOutlineColor: control.Material.accentColor
                          placeholderTextWidth: Math.min(placeholder.width, placeholder.implicitWidth) * placeholder.scale
                          placeholderTextHAlign: control.effectiveHorizontalAlignment
                          controlHasActiveFocus: control.activeFocus
                          controlHasText: control.length > 0
                          placeholderHasText: placeholder.text.length > 0
                          horizontalPadding: control.Material.textFieldHorizontalPadding
                      }
                  }
      
      1 Reply Last reply
      0

      • Login

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