在 QML 中设置公共属性值,例如 QSS [英] Set common property value in QML such as QSS

查看:42
本文介绍了在 QML 中设置公共属性值,例如 QSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有 2 个不同的 QML 元素,它们具有共同的属性,例如:

For example i have 2 different QML elemnts with common property such as:

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    Text {
        id: t
        color: "red"
        text: qsTr("Hello World")
        anchors.top: parent.top
    }
    TextInput {
        text: qsTr("Hello all!")
        color: "red"
        anchors.top: t.bottom

    }
}

您可以看到,Text 和 TextInput 具有相同的属性,称为color",具有相同的值.

You can see, that Text and TextInput have equal property called "color" with equal value.

在 QSS 中,我可以使用公共属性值,例如:

In QSS i can use common property value, for example:

QWidget {
   background: "red"
}

以及所有属于 qss 小部件的 QWidget 也将具有红色背景.

and all QWidgets, that belong the qss widget also will have red background.

是否可以在 QML 中设置公共属性?

Is way for set common property in QML?

推荐答案

不支持在 QML 中使用 QSS 进行自定义.但是您可以使用样式对象"方法来设置属性并在您的所有 QML 文件中使用它们.

There is no support for customizing using QSS in QML. But you can use "Style Object" method to set the properties and use them in all your QML files.

在这里,您在Style.qml"文件中定义了一个 Style 对象,其属性定义了该样式.在根组件中实例化,因此它将在整个应用程序中可用.

In this, you define a Style object in a "Style.qml" file, with properties defining the style. Instantiate in the root component, so it will be available throughout the application.

// Style.qml
QtObject {
    property int textSize: 20
    property color textColor: "green"
}

// root component
Rectangle {
    ...
    Style { id: style }
    ...
}

// in use
Text {
    font.pixelSize: style.textSize
    color: style.textColor
    text: "Hello World"
}

您可以在此处找到更多信息.

You can find more information here.

这篇关于在 QML 中设置公共属性值,例如 QSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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