2
A
回答
1
如果你想在QML中声明一个“类”,你必须创建一个新的QML文件。它的名字必须以大写字母开头。你也可以使用C++创建自定义对象,但可能这不是你正在寻找的。
假设您要创建自定义Text
元素,以便文本总是居中并适合给定尺寸。所以,你创建一个名为CustomText.qml文件,并写入:
/* CustomText.qml file */
import QtQuick 2.0
Text {
id: customText
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
clip: true
fontSizeMode: Text.Fit
font.pixelSize: height
wrapMode: Text.WordWrap
minimumPixelSize: 3
color: "black"
/* using these lines you can set custom font loaded from a file */
// font.family: customFont.name
// FontLoader {
// id: customFont
// source: "qrc:/myCustomFont.ttf"
// }
}
现在你可以使用这样的:
/* main.qml file */
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
width: 300
height: 300
Rectangle {
id: rectangle1
color: "lightgrey"
x: 5
y: 5
width: 200
height: 50
CustomText {
anchors.fill: parent
text: "testing custom text object"
}
}
Rectangle {
id: rectangle2
color: "lightgrey"
anchors.left: rectangle1.left
anchors.top: rectangle1.bottom
anchors.topMargin: 5
width: 50
height: 50
CustomText {
anchors.fill: parent
text: "testing custom text object"
}
}
Rectangle {
id: rectangle3
color: "lightgrey"
anchors.left: rectangle2.left
anchors.top: rectangle2.bottom
anchors.topMargin: 5
width: 100
height: 100
CustomText {
anchors.fill: parent
text: "testing custom text object"
}
}
}
这就是它会是什么样子:
相关问题
- 1. 自定义样式Qt快速控件
- 2. qml和c + +与qt快速2应用程序
- 3. Qt,QML ListView和桌面应用程序
- 4. 自定义登录页面CSS样式
- 5. 如何通过Qt样式表自定义Qt应用程序的标题栏?
- 6. Qt快速QML MouseArea自动重复按
- 7. QML中的多页面应用程序
- 8. 在HTML页面中启用自定义文本选择样式
- 9. 应用WPF样式,比如CSS和HTML
- 10. 如何将我的自定义html页面(js和css)添加到wordpress页面
- 11. 从QML调用C++方法(Qt快速应用)
- 12. HTML,JS和CSS桌面应用程序
- 13. 如何快速删除CSS样式
- 14. Facebook自定义页面和应用程序方向?
- 15. CSS样式表和自定义字体
- 16. ng样式和自定义css属性
- 17. 样式化CSS字体,在html页面上有两个自定义字体
- 18. 如何在Qt 5.2 QML应用程序中实现分页
- 19. Facebook和Twitter应用程序发布页面的快捷方式
- 20. 用自定义CSS弹出页面不覆盖页面的CSS
- 21. 在QT 5.6(QML)中自定义滑块
- 22. 木卫四自定义对话框自定义样式的Windows应用程序
- 23. 将样式应用于使用html/css的定义列表
- 24. 自定义MBean HTML页面
- 25. Qt中自定义窗口小部件的自定义样式
- 26. 用自定义CSS覆盖Material2样式
- 27. Qt使用C++和QML的应用程序
- 28. QT /设为Qml快速控制2:无数字格式SPINBOX值
- 29. 自定义iPhone应用程序界面
- 30. 如何使用带有QML信号的自定义Qt类型?
如果从现在开始,每个元素都必须使用绿色字体颜色。我必须为每个元素创建一个自定义QML吗?我想采取一个现有的项目,并以某种方式改变它的风格 –
@DanielSantos不幸的是。您必须手动将每个“文本”更改为“CustomText”,并在其中设置“color:”green“”。 –