以下代码适用于QTextEdit,但它不适用于QPlainTextEdit:
ui->setupUi(this);
ui->plainTextEdit->clear();
ui->plainTextEdit->appendPlainText("example");
ui->plainTextEdit->appendPlainText("example");
QTextCursor cursor = ui->plainTextEdit->textCursor();
QTextBlockFormat textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(textBlockFormat);
ui->plainTextEdit->setTextCursor(cursor);
ui->plainTextEdit->appendPlainText("example");
cursor = ui->plainTextEdit->textCursor();
textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(textBlockFormat);
ui->plainTextEdit->setTextCursor(cursor);
我希望附加文本的对齐方式能够改变,但事实并非如此。