QTextEdit的段落排版
2014-04-09 21:08
323 查看
相关结构就不多说了,排版 字体等使用Html/css属性。
这是默认模板生成的一个例子:
我们关注的就是段落 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);
}
效果图:
这是默认模板生成的一个例子:
<!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);
}
效果图:
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- Qt实现图片移动实例(图文教程)
- QModelIndex/Role/Model介紹<二>
- ok6410开发板移植DirectFB手记
- 【笔记】给Qt内嵌一个Chrome吧
- 在 Qt4 中使用 C++11
- Hello Word ~ v0.2.2 背单词软件发布 -- By WHYPRO
- QT的命名风格
- QT 中的sleep
- QT 串口的监控
- 在VS2010下配置QT和ITK+VTK
- 在Windows下建立QT开发环境
- Qmake does not support build directories below the source directory
- MFC调用Qt生成的dll
- QT 调用 DLL 方法
- QT主要类QWidget
- QT 笔记:禁止调整窗口大小
- WidgetFlags
- 转载QT中文论坛的一个帖子,版本比较老了,但是说的比较全,有了框架再干活比较顺。