QTextEdit的段落排版
来源:互联网 发布:艾默生网络能源vertiv 编辑:程序博客网 时间:2024/04/28 06:56
相关结构就不多说了,排版 字体等使用Html/css属性。
这是默认模板生成的一个例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"><html><head><meta name="qrichtext" content="1" /><style type="text/css">p, li { white-space: pre-wrap; }</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;"><p style=" margin-top:100px; margin-bottom:100px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">this is my text block</p></body></html>
我们关注的就是段落 p标签。
margin-top等可用来控制行距,
-qt-block-indent段落缩进
text-indent行缩进
如果要控制字体,颜色,等信息,则使用<span> , 如
<span style=" font-style:italic;"></span>
下面的两个方法是用来模拟飞秋聊天窗口:
void P2PChatWnd::ChangeHTMLSend2View(){ //strikeout , underline,经测试toHtml方法不会添加这两个属性 bool underline = false; bool strikeout = false; strikeout = ui->textEdit->currentFont().strikeOut(); underline = ui->textEdit->currentFont().underline(); const QString & src = ui->textEdit->toHtml(); QString strView = ui->textEditShow->toPlainText(); if( strView.isEmpty() ){ strView = "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">" "p, li { white-space: pre-wrap; }" "</style></head><body style=\" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;\">" "</body></html>"; } else{ strView = ui->textEditShow->toHtml(); } //获取所有段落 int start = src.indexOf("<p",0); int end = src.lastIndexOf("</body>"); QString txt = src.mid(start,end-start); //更改行缩进 const QString strTag = "text-indent:" ; int pos = 0; for(;;) { pos = txt.indexOf(strTag,pos+1); if( pos!=-1) txt.replace(pos+strTag.length(),1,'9'); else break; } //获取发送框的字体和颜色信息! pos = src.indexOf("<body",0); int styleend = src.indexOf(">",pos+1); QString fontinf("<span "); fontinf += src.mid(pos+strlen("<body"),styleend-pos-4); if(underline) fontinf.insert(fontinf.length()-2,"text-decoration: underline;"); if(strikeout) fontinf.insert(fontinf.length()-2,"text-decoration: line-through;"); //插入字体信息到段落 int tail=0; tail = txt.indexOf(">",0); for(; tail!=-1;) { txt.insert(tail+1,fontinf); tail = txt.indexOf("</p>",tail+1); txt.insert(tail,"</span>"); tail = txt.indexOf(">",tail+strlen("</span></p>")+1); } int despos = strView.lastIndexOf("</body>"); strView.insert(despos,txt); ui->textEditShow->setHtml(strView);}void P2PChatWnd::InsertTalker(const QString & tag){ QString strView = ui->textEditShow->toPlainText(); if( strView.isEmpty() ){ strView = "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">" "p, li { white-space: pre-wrap; }" "</style></head><body style=\" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;\">" "</body></html>"; } else{ strView = ui->textEditShow->toHtml(); } QString p = QString("<p style=\" margin-top:5px; margin-bottom:5px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600; color:#072cff;\">%1</span></p>").arg(tag); int despos = strView.lastIndexOf("</body>"); strView.insert(despos,p); ui->textEditShow->setHtml(strView);}
效果图:
0 0
- QTextEdit的段落排版
- PHOTOSHOP段落文字排版
- 英文段落排版
- 段落排版-css
- CSS 文字、段落排版
- 【Html】段落排版--缩进
- 【Html】段落排版--对齐
- 段落排版--缩进
- 段落排版--对齐
- iOS 段落文字排版的属性与细节
- iOS 段落文字排版的属性与细节
- 【Web 前端】文字排版、段落排版
- HTML/CSS之段落排版
- CSS格式化之段落排版
- CSS格式化排版 文字+段落
- Foundation-几种段落排版格式NSMutableParagraphStyle
- 段落排版--行间距(行高)
- 段落排版--中文字间距、字母间距
- hdu4044 GeoDefense 树形背包
- sql存储过程
- ActionBar 创建自定义的Action Provider
- openfire研究之部署连接管理器(connection manager)
- 山建1050
- QTextEdit的段落排版
- 跑酷游戏策划
- IOS中数组和字典基础
- Cygwin下VI命令使用-修改文件
- hdu 4571 Travel in time(Floyd+记忆化)
- bootstrap-daterangepicker-master相关用法
- HDU-1030-DeltaWave
- POJ 1128
- C#格式化日期时间