如何将样式应用于QML中的TextField?似乎是“风格".该属性不可用 [英] How do I apply the style to a TextField in QML? It seems "style" attribute isn't available

查看:667
本文介绍了如何将样式应用于QML中的TextField?似乎是“风格".该属性不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些样式应用于正在使用的新qt 5.7应用程序,而以下内容根本无法正常工作.它给出了错误: qrc:/SignInView.qml:67无法分配给不存在的属性样式" 出于相同的原因,我无法在设计模式下对其进行编辑.

I am trying to apply some styles to a new qt 5.7 application I am working on and the following is not working at all. It gives the error: qrc:/SignInView.qml:67 Cannot assign to non-existent property "style" And I can't edit it in design mode for the same reasons.

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
                    }
                }
            }
        }
    }
}

我一直在尝试遵循以下示例:

I have being trying to follow this example:

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

它在Qt Creator中的style属性处失败,并给出错误的提示:样式不存在. 我认为这与我的库未加载或我已设置的环境有关. 我在按钮或其他任何地方都没有样式.我以为如果有进口货就可以,但是没有.

It fails at the style attribute in the Qt Creator giving the error that style doesn't exist. I assume it's something with my libraries not loading or maybe the environment I have setup. I don't have style in buttons or anywhere else either. I assumed if I had the imports it would work but it's not.

有关SO的相关问题在这里: QML-如何更改TextField字体大小 但是在这里看来似乎行得通.

A related issue on SO is here: QML - How to change TextField font size But here it seems to just work.

推荐答案

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

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:

  • Differences between Qt Quick Controls
  • Styling Qt Quick Controls 2
  • Customizing Qt Quick Controls 2

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

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" }
}

您可以在此处上详细了解历史.特别是,这篇文章重点介绍Qt Quick Controls 2的基本结构变化.

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

这篇关于如何将样式应用于QML中的TextField?似乎是“风格".该属性不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆