QT QTextBorwser设置右对齐

在使用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);
}
c++
打赏
  • 微信
  • 支付宝
评论
来发评论吧~
···

歌手: