How do I apply the style to a TextField in QML? It seems “style” attribute isn’t available

In Qt Quick Controls 2, there is no such property as TextField::style. In general, there is no way to use the style objects from Qt Quick Controls 1 with Qt Quick Controls 2. The APIs between the two major versions of Qt Quick Controls are not compatible. See the following documentation pages for more details:

A new API-incompatible major version was introduced, because there is basically no way to make the heavily Loader-based architecture of Qt Quick Controls 1 perform reasonably well. Therefore all that dynamic loading of Components was ditched in Qt Quick Controls 2. The delegates that used to be dynamically instantiated from Components provided by a dynamically loaded style object are now part of the control instead, instantiated “in place”. In essence:

TextField {
    style: TextFieldStyle {
        textColor: "white"
        background: Rectangle { color: "black" }
    }
}

vs.

TextField {
    color: "white"
    background: Rectangle { color: "black" }
}

You can read more about the history here. In particular, this post highlights the fundamental structural changes in Qt Quick Controls 2.

Leave a Comment