Reputation: 8444
If I have a QTextEdit box, how can I align different pieces of text within the box in different ways? For example, I would like to have one sentence be aligned-left, and the next sentence in the box be aligned-right. Is this possible? If not, how might I achieve this effect in Qt?
Upvotes: 4
Views: 19611
Reputation: 18524
As documentation said:
void QTextEdit::setAlignment(Qt::Alignment a) [slot]
Sets the alignment of the current paragraph to a
. Valid alignments are Qt::AlignLeft
, Qt::AlignRight
, Qt::AlignJustify
and Qt::AlignCenter
(which centers horizontally).
Link: https://doc.qt.io/qt-5/qtextedit.html#setAlignment
So as you can see you should provide some alignment to each paragraph.
Little example:
QTextCursor cursor = ui->textEdit->textCursor();
QTextBlockFormat textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignRight);//or another alignment
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);
Which result I get on my computer?
Or something closer to your question:
ui->textEdit->clear();
ui->textEdit->append("example");
ui->textEdit->append("example");
QTextCursor cursor = ui->textEdit->textCursor();
QTextBlockFormat textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);
ui->textEdit->append("example");
cursor = ui->textEdit->textCursor();
textBlockFormat = cursor.blockFormat();
textBlockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(textBlockFormat);
ui->textEdit->setTextCursor(cursor);
Result:
Upvotes: 6
Reputation: 1945
I tried the other answer, it does work, but it seems too much code just for text alignment. Also it does not work for text font, which requires character format, instead of block format.
Then I realized that, the easiest solution is to just use html code. All you need to do is just putting html code into setText()
. The html code can be very simple, if you only need to change text alignment, it would be something like:
"<p align='left'>text1</p><p align='center'>text2</p><p align='right'>text3</p>"
And you can add css code to change the font size, bold, color, etc.
Upvotes: 0
Reputation: 1562
To left align to a textbox "TerminalOutput":
string Wibble="wibble";
TerminalOutput->append(QString::fromStdString(Wibble));
TerminalOutput->setAlignment(Qt::AlignLeft);
To right align to a textbox:
string Wobble="wobble";
TerminalOutput->append(QString::fromStdString(Wobble));
TerminalOutput->setAlignment(Qt::AlignRight);
Now, sometimes, and this happens with Kosovan's answer too, the alignment in my code sets the previous line instead of the current. This is inexplicable to me. I cannot work out why this is. If anyone knows why this is, please leave a comment because it's driving me nuts.
Edit, I found the problem. So the alignment works fine, until you select some text with the cursor. The instant you do that, the alignment stops aligning the previous line and then decides to affect the following line instead of the previous and this affects all other append formats henceforth. In fact clicking anywhere inside the textbox, will do this, it must be that it's interfering with qt's internal notion of cursor position.
I fixed this problem by doing the following before appending to the textbox:
QTextCursor newCursor = TerminalOutput->textCursor();
newCursor.movePosition(QTextCursor::End);
TerminalOutput->setTextCursor(newCursor);
And what that does is just moves the cursor to the end of the textbuffer so that when you append, it clears out any cursor position of a user clicking anywhere within the textbox, and this solves my strange little problem.
Upvotes: 0