如何在QML中将样式应用于TextField?似乎"风格"属性不可用

Enc*_*ass 6 qt qml qtquick2 qtquickcontrols2

我正在尝试将一些样式应用于我正在处理的新qt 5.7应用程序,以下内容根本不起作用.它给出了错误:qrc:/SignInView.qml:67无法分配给不存在的属性"style"由于同样的原因我无法在设计模式下编辑它.

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.4

Page {
    id: page1
    ColumnLayout {
        id: columnLayout1
        height: 100
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.top: parent.top

        Label {
            text: qsTr("Label")
            font.pointSize: 16
            horizontalAlignment: Text.AlignHCenter
            Layout.fillWidth: true
        }

        Image {
            id: image1
            width: 200
            height: 200
            Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
            fillMode: Image.PreserveAspectCrop
            anchors.horizontalCenter: parent
            source: "qrc:/qtquickplugin/images/template_image.png"

            Button {
                id: button1
                text: qsTr("Button")
                anchors.bottomMargin: 10
                anchors.rightMargin: 10
                anchors.bottom: parent.bottom
                anchors.right: parent.right
            }
        }

        Rectangle {
            id: field1
            width: 200
            height: 40
            color: "#ffffff"
            Layout.fillWidth: true



            Label {
                id: label1
                text: qsTr("Full Name")
                anchors.topMargin: 0
                anchors.left: parent.left
                anchors.leftMargin: 5
                anchors.top: parent.top
            }
            TextField {
                style: TextFieldStyle {
                    textColor: "black"
                    background: Rectangle {
                        radius: 2
                        implicitWidth: 100
                        implicitHeight: 24
                        border.color: "#333"
                        border.width: 1
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图遵循这个例子:

http://doc.qt.io/qt-5/qml-qtquick-controls-styles-textfieldstyle.html

它在Qt Creator中的style属性失败,给出了样式不存在的错误.我认为这是我的库没有加载或可能我设置的环境.我没有按钮或任何其他地方的风格.我假设如果我有进口它会起作用,但事实并非如此.

关于SO的一个相关问题是:QML - 如何更改TextField字体大小 但是在这里似乎只是工作.

jpn*_*rmi 18

在Qt Quick Controls 2中,没有这样的属性TextField::style.通常,无法使用Qt Quick Controls 1中的样式对象与Qt Quick Controls 2. Qt Quick Controls的两个主要版本之间的API不兼容.有关详细信息,请参阅以下文档页面:

引入了一个新的API不兼容的主要版本,因为基本上没有办法使Qt Quick Controls 1的基于Loader的重度架构表现得相当好.因此,Component在Qt Quick Controls 2中抛弃了所有s的动态加载.过去从Component动态加载的样式对象提供的动态实例化的委托现在是控件的一部分,而是"就地"实例化.在本质上:

TextField {
    style: TextFieldStyle {
        textColor: "white"
        background: Rectangle { color: "black" }
    }
}
Run Code Online (Sandbox Code Playgroud)

TextField {
    color: "white"
    background: Rectangle { color: "black" }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处阅读有关历史的更多信息.特别是,这篇文章强调了Qt Quick Controls 2的基本结构变化.