我正在尝试将一些样式应用到我正在开发的新 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
}
}
}
}
}
}
我一直在尝试遵循这个例子:
http://doc.qt.io/qt-5/qml-qtquick-controls-styles-textfieldstyle.html
它在 Qt Creator 中的样式属性处失败,给出样式不存在的错误。 我认为这是我的库未加载或者我设置的环境的问题。 我在按钮或其他任何地方都没有风格。我以为如果我有导入它就会起作用,但事实并非如此。
SO 的相关问题在这里:QML - How to change TextField font size 但在这里它似乎起作用了。
最佳答案
在 Qt Quick Controls 2 中,没有 TextField::style
这样的属性。一般来说,无法将 Qt Quick Controls 1 和 Qt Quick Controls 2 中的样式对象一起使用。两个主要版本的 Qt Quick Controls 之间的 API 不兼容。有关更多详细信息,请参阅以下文档页面:
引入了一个新的与 API 不兼容的主要版本,因为基本上没有办法让 Qt Quick Controls 1 的基于 Loader 的架构表现得相当好。因此,Qt Quick Controls 2 中放弃了所有 Component
的动态加载。过去从动态加载的样式对象提供的 Component
动态实例化的委托(delegate)现在变成了相反,控件的一部分被“就地”实例化。本质上:
TextField {
style: TextFieldStyle {
textColor: "white"
background: Rectangle { color: "black" }
}
}
对比
TextField {
color: "white"
background: Rectangle { color: "black" }
}
您可以阅读更多有关历史的信息here 。特别是this post突出显示 Qt Quick Controls 2 中的基本结构变化。
关于qt - 如何将样式应用到 QML 中的 TextField?似乎 "style"属性不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052139/