QTextEdit和QTextDocument(ZZ)

来源:互联网 发布:2017最新网络流行语 编辑:程序博客网 时间:2024/06/09 17:46

http://blog.csdn.net/rea_1121/article/details/6432064

QTextEdit简介:

它可以显示rich text。 这里的rich text不是指microsoft的rich text,而是用html标签表示的形式。

它通过viewport显示大型文件,而且性能好,可以快速响应用户的操作。

以paragraph和character为基本单位工作。paragraph是格式化的字符串,自动word-wrap以适应容器的大小。一个document由paragraph组成。paragraph可以有自己的alignment属性,paragragh之间由hard line break截断。每个character有自己的字体和颜色等属性。

QTextEdit可以显示图像,列表和表格。必要时会有滚动条以浏览全文。text edit可以显示plain text和html文件。QtWebKit是比较完整的浏览器控件。QLabel也可以显示小段的rich text.

方法:

I. 显示文本:

setHtml(), toHtml(), clear()用来控制内容到html的转换,还有清除内容。

插入内容:insertHtml(), insertPlainText(), append(), paste(). 类QTextCursor可以用来插入table,list,text,还用来创建selection和修改selection

word wrap: 默认的方式是在空白字符处wrap。 setLineWrapMode() 可以指定wrap的模式,像素,几列,从来不wrap,随便wrap.

查找:find()方法用来查找和选定text内的制定字符串。

可以设置QTextDocument的maximumBlockCount属性来控制文档最多显示的段落数。

II.编辑文本:

设置当前character的属性:setFontItalic (), setFontWeight (), setFontUnderline (), setFontFamily (),setFontPointSize (), setTextColor () and setCurrentFont ().

设置当前paragraph的属性:

得到类QTextCursor来操控selection: textCursor(),返回当前可见的cursor; 创建一个QTextCursor对象setTexctCursor()。 copy和cut方法会把当前选中的文本放到剪贴板中。currentCharFormatChanged()是个信号,cursor移动而且字符格式改变时会被调用。

得到类QTextDocument来操控:document()。setDocument()可以用来设置document. 当document改变时会有textChanged()信号。 isModified方法可以来检查document是否改变。它也有undo() redo()方法。

还有一部分context menu的方法。

QTextDocument简介:

存放结构化的rich text文件,每个元素都有对应的格式对象。可以通过objectForFormat()来通过格式对象查找元素。

可以通过QTextCursor来编程编辑QTextDocument对象。通过rootFrame()方法得到根节点再遍历其中的元素。如果只是浏览其中的文本文档,就用begin(),end(),findBlock()方法。

文本的格式由documentLayout()决定.metaInformation()来的到文件的元信息。

toPlainText()和toHtml()方法能够得到它text形式的内容和html形式的内容。

QTextCursor简介:

QTextCursor是专门用来协助处理和获取QTextDocument内容的一组api. 它包含的信息既有QTextDocument内的光标位置(position()),以及所做的selection(anchor() 和 position()之间)。current character是指position之前的那个character. current block是指包含position()位置的block.

方法:

setPosition(), movePosition() 可以用来创建selection。取得selection的内容:selectionStart (), selectionEnd (),hasSelection (), clearSelection (), andremoveSelectedText ().

取得format信息:charFormat (), blockFormat ().

设置format信息:setCharFormat (),mergeCharFormat (),setBlockFormat () andmergeBlockFormat (). merge会把本来的格式合并。当当前有selection时,char format会对应到所选中的内容上;即使block没有全选,block format会应用于整个block.

删除文本:deleteChar (), deletePreviousChar (), andremoveSelectedText ().

插入文本:insertText () function, insertBlock (). insertList (),insertTable (),insertImage (),insertFrame ()

action可以分组(被undo/redo当作一个操作):beginEditBlock () and endEditBlock ().