在Qt QML中为许多文本元素指定字体 [英] Specifying font for many Text-elements in Qt QML
问题描述
我有一个通过QML文件指定的小部件。此小部件包含顶层 Rectangle
,其中包含两个列
。这些列
中的每一个都包含许多文本
- 元素。这个QML小部件包装在C ++中的 QDeclarativeView
的子类中。
我想为每个这些文本
-elements。今天我通过指定顶级属性这样做:
属性字符串fontfamily:Arial
property bool fontbold: false
property bool fontitalic:false
property int fontpixelsize:11
property string fontcolor:White
并将每个文本
-elements绑定到这些属性:
文字
{
color:fontcolor
font.family:fontfamily
font.bold:fontbold
font.italic:fontitalic
font.pixelSize:fontpixelsize
...
}
非常优雅和新的领域需要添加每次我需要支持新的东西(例如下划线字体)。我不能声明一个属性类型 font
并绑定到这个(widget是空的,qmlviewer警告期望的类型后的属性)。
有没有更好的方法来为所有 Text
-elements指定字体?
注意!
另一种可能性是写一个新的QML组件,继承自 Text
a默认设置一些属性:
StyledText.qml
import QtQuick 1.0
文本{
//设置默认值
颜色:blue
font.family:Arial
font.bold:true
font.italic:true
font.pixelSize:12
}
main.qml
import QtQuick 1.0
Rectangle {
Row {
spacing:10
列{
StyledText {
text:Foo1
}
StyledText {
text:Bar1
}
StyledText {
text:Baz1
}
}
列{
StyledText {
text:Foo2
}
StyledText {
text:Bar2
}
StyledText {
text:Baz2
}
}
}
}
I have a widget specified through a QML file. This widget contains a top levelRectangle
which contains two Columns
. Each of these Columns
contains many Text
-elements. This QML widget is wrapped in a subclass of QDeclarativeView
in C++.
I want to specify the font for each of these Text
-elements. Today I do this by specifying top-level properties:
property string fontfamily: "Arial"
property bool fontbold: false
property bool fontitalic: false
property int fontpixelsize: 11
property string fontcolor: "White"
and bind each Text
-elements to these properties:
Text
{
color: fontcolor
font.family: fontfamily
font.bold: fontbold
font.italic: fontitalic
font.pixelSize: fontpixelsize
...
}
This isn't very elegant and new fields needs to be added every time I need support for something new (e.g. underlined fonts). I have not been able to declare a property of type font
and bind to this instead (widget is blank and qmlviewer warns about "expected type after property").
Is there a better way to specify a font for all Text
-elements?
Note! I'm handwriting the QML files.
Another possibility is to write a new QML component, that inherits from Text
an sets some properties by default:
StyledText.qml
import QtQuick 1.0
Text {
// set default values
color: "blue"
font.family: "Arial"
font.bold: true
font.italic: true
font.pixelSize: 12
}
main.qml
import QtQuick 1.0
Rectangle {
Row {
spacing: 10
Column {
StyledText {
text: "Foo1"
}
StyledText {
text: "Bar1"
}
StyledText {
text: "Baz1"
}
}
Column {
StyledText {
text: "Foo2"
}
StyledText {
text: "Bar2"
}
StyledText {
text: "Baz2"
}
}
}
}
这篇关于在Qt QML中为许多文本元素指定字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!