QT QTextBorwser设置右对齐
在使用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);
}
c++