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
    评论
QTextEdit是一个常见的用于输入和编辑文本的Qt组件。根据您的问题,我可以简要地解释一下QTextEdit的拖拽功能。 QTextEdit支持两种类型的拖拽操作:拖拽进QTextEdit和从QTextEdit拖拽出去。 首先,让我们来看看拖拽进QTextEdit的情况。如果您想将一些文本或者其他可拖拽的数据拖拽到QTextEdit中,您需要先将数据设置为可拖拽的项。您可以使用QDrag类来实现这个功能。具体来说,您可以在源组件上调用QDrag开始拖拽操作,并设置要拖拽的数据。在QTextEdit中,您可以重写dragEnterEvent()和dropEvent()方法来处理拖拽操作。在dragEnterEvent()中,您可以检查拖拽的数据类型,并决定是否接受这个拖拽操作。在dropEvent()中,您可以将拖拽的数据插入到QTextEdit中。 其次,如果您想从QTextEdit拖拽出数据,您可以使用setDragEnabled()方法来启用此功能。然后您可以选择要拖拽的文本并拖动鼠标来开始拖拽操作。通过重写dragEnterEvent()和dragMoveEvent()方法,您可以确定拖拽操作是否允许,并设置拖拽的数据类型等信息。如果您希望在拖拽操作完成后执行某些操作,您可以重写dropEvent()方法。 总体而言,QTextEdit的拖拽功能提供了一种方便的方法来与其他组件或应用程序之间交换数据。通过了解QDrag和相关的事件处理方法,您可以自由地控制拖拽操作的行为和数据的传输。希望这个回答对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值