QTextEdit的段落排版

9 篇文章 0 订阅


相关结构就不多说了,排版 字体等使用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);
}





效果图:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值