当前位置: 首页 > news >正文

QML TextEdit组件

TextEdit是QML中用于多行文本编辑的核心组件,相比单行输入的TextInput,它提供了更丰富的文本处理能力,包括多行编辑、富文本支持、光标控制等功能。本文将全面介绍TextEdit的核心特性、使用方法、样式定制以及实际应用技巧,帮助开发者掌握这一重要组件的使用。

TextEdit基础介绍

TextEdit是Qt Quick模块提供的多行文本编辑组件,类似于传统UI框架中的QTextEdit。与单行输入的TextInput不同,TextEdit专注于多行文本的编辑和显示。

基本使用

import QtQuick 2.15TextEdit {id: textEditwidth: 300height: 200text: "初始文本"font.pixelSize: 16color: "black"wrapMode: TextEdit.WordWrap // 自动换行
}

关键特性​:

  • 支持多行文本编辑和显示
  • 提供富文本(HTML子集)和纯文本两种格式
  • 内置多种换行模式控制
  • 支持文本选择、复制粘贴等编辑操作
  • 可通过validator实现输入验证
  • 提供光标位置控制、选区管理等功能

核心属性详解

1. 文本内容与格式

  • text​:编辑框中的文本内容,支持静态文本和动态绑定
  • textFormat​:文本格式,可选值:
    • TextEdit.PlainText:纯文本(默认)
    • TextEdit.RichText:富文本(HTML子集)
    • TextEdit.AutoText:自动检测文本格式
  • color​:文本颜色,支持颜色名称或十六进制值
  • font​:字体样式组,包含多个子属性:
    font {family: "Arial"  // 字体pixelSize: 16    // 字号bold: true       // 加粗italic: true     // 斜体underline: true  // 下划线
    }

2. 布局与显示

  • wrapMode​:文本换行模式
    • TextEdit.NoWrap:不自动换行
    • TextEdit.WordWrap:在单词边界处换行(推荐)
    • TextEdit.WrapAnywhere:在任何字符处换行
    • TextEdit.Wrap:同WordWrap
  • horizontalAlignment​/​verticalAlignment​:水平和垂直对齐方式
  • contentWidth​/​contentHeight​:文本内容的实际宽度和高度(可能超出可视区域)
  • lineCount​:只读属性,返回文本行数

3. 交互与选择

  • readOnly​:是否为只读模式
  • selectByMouse​:是否允许鼠标选择文本
  • selectionColor​:选中文本的背景色
  • selectedTextColor​:选中文本的颜色
  • cursorPosition​:当前光标位置
  • cursorVisible​:光标是否可见

滚动与可视区域管理

TextEdit本身不实现滚动行为,需要配合Flickable使用:

Flickable {id: flickwidth: 300; height: 200contentWidth: textEdit.paintedWidthcontentHeight: textEdit.paintedHeightclip: trueTextEdit {id: textEditwidth: flick.widthheight: flick.heightwrapMode: TextEdit.Wrap// 确保光标可见onCursorRectangleChanged: ensureVisible(cursorRectangle)}function ensureVisible(r) {if (contentX >= r.x)contentX = r.x;else if (contentX+width <= r.x+r.width)contentX = r.x+r.width-width;if (contentY >= r.y)contentY = r.y;else if (contentY+height <= r.y+r.height)contentY = r.y+r.height-height;}
}

 代码示例:

import QtQuickWindow {width: 640height: 480visible: truetitle: qsTr("Hello World")TextEdit {id: textEdit1width: 240text: "Hello World"font.family: "Source Sans Pro Black"font.pointSize: 20color: "blue"focus: trueselectedTextColor: "yellow" //选中字体颜色selectionColor: "darkgreen" //选中颜色onEditingFinished: {console.log("onEditingFinished: " + text)}onTextChanged: {console.log("onTextChanged: " + text)if(text === "2024"){console.log("Your password is right.")}}}Rectangle {id: textEdit2Rectanglewidth: 300height: 200anchors.left: textEdit1.rightanchors.leftMargin: 20x: 260y: 0color: "lightgreen"radius: 20//用于实现可滚动/可拖动的交互区域,其核心功能是允许用户通过触摸或鼠标拖拽手势浏览超出可视区域的内容,并支持惯性滑动效果。Flickable {     //即超出文本输入框可以滑动显示id: flickwidth: 300height: 200contentWidth: textEdit2.contentWidthcontentHeight: textEdit2.contentHeightclip: truefunction ensureVisible(r){if (contentX >= r.x)contentX = r.x;else if (contentX+width <= r.x+r.width)contentX = r.x+r.width-width;if (contentY >= r.y)contentY = r.y;else if (contentY+height <= r.y+r.height)contentY = r.y+r.height-height;}TextEdit {id: textEdit2width: 300height: 200text: "Hello World"font.pointSize: 32color: "red"focus: truewrapMode: TextEdit.Wrap  //设置换行padding: 10  //设置上下左右间距10像素onLineCountChanged: {  //输入行变化,即换行触发console.log("lineCount: " + lineCount)}}}}Rectangle {id: textEdit3Rectanglewidth: 300height: 200anchors.top: textEdit2Rectangle.bottomanchors.topMargin: 20color: "lightgray"radius: 5TextEdit {id: textEdit3anchors.fill: parenttext: "Hello World"font.pointSize: 32color: "blue"focus: truewrapMode: TextEdit.Wrappadding: 10horizontalAlignment: TextEdit.AlignHCenter}}
}

效果演示:

http://www.dtcms.com/a/268509.html

相关文章:

  • 【BFS】 P10864 [HBCPC2024] Genshin Impact Startup Forbidden II|普及+
  • 使用3.20.3版本的protoc编译proto2和proto3
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(四十一) -> 获取自定义编译参数
  • 百度开源文心 4.5 系列开源大模型 GitCode 本地化部署,硅基流动:文心 vs. DeepSeek vs. Qwen 3.0 深度测评
  • 【apply from: “$flutterRoot/packages/flutter_tools/gradle/flutter.gradle“作用】
  • 云计算领域“XaaS”是什么?
  • 使用CocoaPods集成第三方SDK - 从零开始完整指南
  • 开源 C# .net mvc 开发(七)动态图片、动态表格和json数据生成
  • H3初识——入门介绍之路由、路由元数据
  • Maven 依赖管理中的 <optional> 与 <scope>标签
  • 管道机器人手臂机械结构设计cad【8张】+三维图+设计说明书+绛重
  • PDF 上传并保存到 MinIO 数据库
  • 基于Python实现LSTM对股票走势的预测
  • 机器学习知识
  • 医疗AI底层能力全链条工程方案的深度分析:从技术突破到临床应用
  • Mask机制​​中的​​Padding Mask​​ 和 ​​Sentence Mask
  • DCL-2-权限控制
  • 项目进度受上游依赖影响大,如何降低风险
  • 国民经济行业分类 GB/T 4754—2017 (PDF和exce版本)
  • .NET9 实现 JSON 序列化和反序列化(Newtonsoft.Json System.Text.Json)性能测试
  • Mysql8.0高可用集群架构实战
  • MySQL 8.0 OCP 1Z0-908 题目解析(21)
  • 熟练掌握ModbusTCP转PROFINET网关的互转技术
  • 深入解析迭代器模式:优雅地遍历聚合对象元素
  • 数据挖掘:深度解析与实战应用
  • IRF堆叠技术的主要优势
  • 学车笔记6
  • 李宏毅genai 笔记:预训练-对齐
  • 【Pyhton】文件读取:读取整个(大型)文件
  • 锁和事务的关系