在 QML 中设置公共属性值,例如 QSS

编程入门 行业动态 更新时间:2024-10-21 15:53:23
本文介绍了在 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

本文发布于:2023-11-14 00:10:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   QML   QSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!