QT QTextBorwser使用setText时设置右对齐
需求描述
QT QTextBorwser使用setText时设置右对齐
在使用QTextBrowser的setText方法时
使用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);
}

浙公网安备 33010602011771号