QT QTextBorwser使用setText时设置右对齐

需求描述

QT QTextBorwser使用setText时设置右对齐

在使用QTextBrowsersetText方法时

使用setAlignment(Qt::AlignRight)无法实现全部文字右对齐的效果

但是如果使用setHtml会有效果

但是实际场景需要setText的时候,就会出现setAlignment失效的情况

这里通过QTextCursor来设置BlockFormat来实现右对齐

{
    QTextBrowser = ui->textBrowser;
    textBrowser->setText(text);
    QTextCursor  cursor = textBrowser->textCursor();
    cursor.select(QTextCursor::Document);
    QTextBlockFormat format;
    format.setAlignment(Qt::AlignRight);
    cursor.setBlockFormat(format);
    cursor.clearSelection();
    textBrowser->setTextCursor(cursor);
}
posted @ 2023-04-26 11:39  续加仪  阅读(160)  评论(0)    收藏  举报  来源