C++图形用户界面开发框架Qt 6.x - 在QML中显示文本

显示和格式化文本

要在QML中显示文本,请创建一个Text项并将text属性设置为您希望显示的文本,Text项现在将显示该文本。

可以在Text项目上设置几个属性来设置整个文本块的样式,这些包括颜色、字体系列、字体大小、粗体和斜体。

富文本(如标记)可用于使用Text项选择性地设置文本特定部分的样式,将 Text::textFormat 设置为 Text.StyledText来使用此功能。

布置文本

默认情况下,除非文本包含嵌入的换行符,否则会将文本显示为单行。要换行,请设置 wrapMode 属性并为文本提供明确的换行宽度。 如果没有明确设置宽度或高度,读取这些属性将返回文本边界矩形的参数(如果您已经明确设置宽度或高度,仍然可以使用paintedWidth和paintedHeight)。考虑到这些参数,文本可以像任何其他项目一样定位。

示例代码

 

import QtQuick

Item {
id: root
width: 480
height: 320

Rectangle {
color: "#272822"
width: 480
height: 320
}

Column {
spacing: 20

Text {
text: 'I am the very model of a modern major general!'

// color can be set on the entire element with this property
color: "yellow"

}

Text {
// For text to wrap, a width has to be explicitly provided
width: root.width

// This setting makes the text wrap at word boundaries when it goes
// past the width of the Text object
wrapMode: Text.WordWrap

// You can use \ to escape quotation marks, or to add new lines (\n).
// Use \\ to get a \ in the string
text: 'I am the very model of a modern major general. I\'ve information \
vegetable, animal and mineral. I know the kings of england and I \
quote the fights historical; from Marathon to Waterloo in order categorical.'

// color can be set on the entire element with this property
color: "white"

}

Text {
text: 'I am the very model of a modern major general!'

// color can be set on the entire element with this property
color: "yellow"

// font properties can be set effciently on the whole string at once
font { family: 'Courier'; pixelSize: 20; italic: true; capitalization: Font.SmallCaps }

}

Text {
// HTML like markup can also be used
text: '<font color="white">I am the <b>very</b> model of a modern <i>major general</i>!</font>'

// This could also be written font { pointSize: 14 }. Both syntaxes are valid.
font.pointSize: 14

// StyledText format supports fewer tags, but is more efficient than RichText
textFormat: Text.StyledText
}
}
}

 

用例 - QML中的动画

国际化和可扩展性

在处理文本时,应用程序必须考虑各种主题,例如设备的方向和语言设置。

Qt商用组件推荐

  • QtitanRibbon - Ribbon UI组件:是一款遵循Microsoft Ribbon UI Paradigm for Qt技术的Ribbon UI组件,QtitanRibbon致力于为Windows、Linux和Mac OS X提供功能完整的Ribbon组件。
  • QtitanChart - Qt类图表组件:是一个C ++库,代表一组控件,这些控件使您可以快速地为应用程序提供漂亮而丰富的图表。
  • QtitanDataGrid - Qt网格组件:提供了一套完整的标准 QTableView 函数和传统组件无法实现的独特功能。使您能够将不同来源的各类数据加载到一个快速、灵活且功能强大的可编辑网格中,支持排序、分组、报告、创建带状列、拖放按钮和许多其他方便的功能。
  • QtitanNavigation:QtitanNavigationDesignUI 组件是一组 GUI 控件,它实现了菜单、导航框、命令栏等导航界面,并让您以更少的滚动和点击次数有效地查看所有实体(工作区、网格或其他项目)。
  • QtitanDocking:允许您像 Visual Studio 一样为您的伟大应用程序配备可停靠面板和可停靠工具栏。黑色、白色、蓝色调色板完全支持 Visual Studio 2019 主题!

Qt技术交流群:166830288      欢迎一起进群讨论

更多Qt产品教程、下载、正版授权资讯,请点击获取

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章