顯示和格式化文本
要在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 } } }
國際化和可擴展性
在處理文本時,應用程序必須考慮各種主題,例如設備的方向和語言設置。
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 歡迎一起進羣討論