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