QLabel text set line spacing

Due to work requirements, it is necessary to use QLabel to display text data. After finishing, it is found that the text line spacing of each line seems to be fixed, and Qt does not expose the API to set, so we need to manually add '\n', but \n is just a newline , and the line spacing cannot be customized. After consulting online information, I found that it can be done with css. The code is as follows:

        

    /* 以下代码必须要按顺序,否则自动换行/自适应大小可能失败 */

    ui->label->setWordWrap(true);//label自动换行

    QString msg = "111啊啊啊啊啊啊啊啊啊啊啊啊啊1111啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊                                           啊啊111";

    //25px是行间距,意思是每一行间距25个像素,其他的直接复制吧,我对前端语言也不了解
    QString qsShow = "<p style='line-height:25px; width:100% ; white-space: pre-wrap; '>" + msg + "</p>";

    ui->label->setText(qsShow);
    ui->label->adjustSize();//label根据内容自适应大小

Guess you like

Origin blog.csdn.net/qq_25704799/article/details/123560360