QtQuick实现文本编辑和显示文本行号-改进版
来源:互联网 发布:mac ps厘米改成像素 编辑:程序博客网 时间:2024/06/12 08:23
可以查看这个简单的项目
QtQuickEditor
先上张效果图
现在简单的说说代码的
主要讲讲TextView的代码
下面的代码是一个自定义Button的控件
/*work for desktop and harmattan * *by qyvlik *qyvlik@qq.com *2014/10/25/11:33 *China **/import QtQuick 1.1Rectangle { id: root property alias text: name.text signal click; width: 100 height: 50 color: "yellow" radius: height/10 Text { id:name anchors.centerIn: parent text:"click" } MouseArea { anchors.fill: parent id:mouse } Component.onCompleted: { mouse.clicked.connect(root.click) }}
接下是使用TextView了
/*work for desktop and harmattan *some bug,can't auto fix TextEdit width * *by qyvlik *qyvlik@qq.com *2014/10/25/11:33 *China **/import QtQuick 1.1Item { id:root property alias text: showText.text property alias readOnly: showText.readOnly property int textPointSize : 20 property bool hold: false property int lineLength property alias textScale: showText.scale Flickable { id: flickable anchors{ right: parent.right rightMargin: 1 left: parent.left leftMargin: 1 bottom: parent.bottom bottomMargin: 10 top:parent.top topMargin: 2 } contentHeight :showText.height contentWidth: showText.width//lineLength enabled: !hold smooth: true Column{ id:showLineCount anchors.left: parent.left Repeater { model: showText.lineCount; Rectangle { width: lineNumberWidth(showText.lineCount) +10 height:temp.height; /* in desktop TextEdit and Text, they 's font.pointSize is 8 TextEdit's height is 13(one line) Text's height is 11(one line) */ color: "yellow" Rectangle { property bool flag:false anchors.left: parent.left anchors.verticalCenter: parent.verticalCenter width: 10 height: 10 color:"yellow" radius:5 MouseArea{ anchors.fill: parent onClicked: { if(parent.flag) { parent.color = "yellow"; parent.flag = false; } else { parent.color = "green"; parent.flag = true; } } } } Text { id:showLineNumber font.pointSize: textPointSize; anchors{ bottom:parent.bottom bottomMargin: 1 horizontalCenter: parent.horizontalCenter } text:index+1 color:"red" } } } } TextEdit { id:showText width: root.width anchors{ left:showLineCount.right } font.pointSize:textPointSize; wrapMode:TextEdit.NoWrap textFormat:TextEdit.PlainText focus: true activeFocusOnPress:true } } TextEdit{ id:temp font.pointSize: textPointSize; visible: false text:qsTr("hello world") /* in desktop TextEdit and Text, they 's font.pointSize is 8 TextEdit's height is 13(one line) Text's height is 11(one line) */ } Rectangle { id: verticalScrollbar //竖直 anchors.right: parent.right anchors.rightMargin: 2 y: flickable.visibleArea.yPosition * flickable.height width: 5 height : flickable.visibleArea.heightRatio * flickable.height radius: 2 color: "#b19393" } Rectangle { id: horizontalScrollbar //水平 anchors.bottom: parent.bottom anchors.bottomMargin: 2 x: flickable.visibleArea.xPosition * flickable.width height: 5 width : flickable.visibleArea.widthRatio * flickable.width radius: 2 color: "#b19393" } function lineNumberWidth(lineCount) { var width = 1; var space = 0; while(lineCount >= 10) { lineCount /= 10; ++width; } return space = width * 20 } function clearText () { text = "" } function fixViewWidth(text) { var i=0; var maxWidth = 0; var tempArray = new Array(); var tempstr1 = new String(); tempArray = text.split("\n"); tempstr1 = tempArray[i]; while(tempstr1) { if(maxWidth<=tempstr1.length) { maxWidth=tempstr1.length; console.log("\t<<"+i+">>\n"); } i+=1; tempstr1 = tempArray[i]; } delete tempstr1; delete tempArray; return maxWidth; } function fixWidth(){ flickable.contentWidth = fixViewWidth(showText.text)*16;//Chinese need two width console.log("view width is:"+flickable.contentWidth); }}
三个函数
一个是clearText()清除文本内容
一个是行号显示区域的宽度调整
一个是TextView的contentWidth的调整fixWidth()
直接上main.qml的代码
/*work for desktop and harmattan *some bug,can't auto fix TextView width * *by qyvlik *qyvlik@qq.com *2014/10/25/11:33 *China **/import QtQuick 1.1import "component"Rectangle { width: 360 height: 360 TextView{ id:textView anchors{ fill:parent bottomMargin: 50 } textPointSize:10 } Row { z:10 id:toolBar anchors{ bottom: parent.bottom bottomMargin: 2 } spacing: 10 Button{ id:button text:"edit "+!textView.readOnly onClick: { if(textView.readOnly) { textView.readOnly = false; } else { textView.readOnly = true; } textView.fixWidth(); } } Button{ text:"clear" onClick: { textView.clearText() } } Button{ text:"hold "+textView.hold onClick: { if(textView.hold) { textView.hold = false } else { textView.hold = true } } } }}
0 0
- QtQuick实现文本编辑和显示文本行号-改进版
- QtQuick实现文本编辑和显示文本行号
- Xilinx SDK文本编辑器行号显示和背景颜色改变
- 通过程序实现SMARTFORMS文本编辑模式在WORD和TEXT文本之间转换
- struts2中富文本编辑器使用ckeditor和ckfinder,实现富文本编辑
- 显示行号的文本输入框
- 如何使Eclipse显示出文本行号
- 学习笔记之vi文本显示行号
- 给富文本编辑后显示的时候在代码块上手动加上行号
- ckeditor ckfinder 实现文本编辑和上传图片
- Linux vi 文本代码时显示行号或不显示行号
- mysql 实现行号和分页
- javascript实现点击文本出现文本编辑状态,移开后变成文本
- 文本编辑框显示光标,屏蔽软键盘和上下文对话框
- iOS富文本编辑(在label里显示文字和图片)
- iOS富文本编辑(在label里显示文字和图片)
- C语言实现/etc/passwd的逐行显示并打印行号和用户名
- QtQuick中解析富文本<初>
- 浅谈磁盘调度算法
- 菜鸟程序员初次搭建MyEclipse与Oracle10g的环境
- adb server is out of date.killing的解决办法
- 好的技术博客、应用总结
- jsp包含html乱码
- QtQuick实现文本编辑和显示文本行号-改进版
- error loading /system/media/audio/ui/Effect_Tick.ogg
- 大话设计模式
- ab 压力测试
- google store 离线下载crx的方法
- 看了此文还不懂傅里叶变换,你来掐死我吧
- 笔试题_Qunar2015一面
- 深搜-连通分量
- 第九周项目5-(1)