VixinG
VixinG

Reputation: 1407

Aligning text on bottom of the QTextEdit

I have a QWidget called side and QTextEdit inside it's sideLayout layout.

chatView = QTextEdit()
chatView.setHtml('<p style="margin: 0px; line-height: 28px;">')
sideLayout.addWidget(tab.chatView, 0, 0, 1, 1)

Below the QTextEdit there's a QLineEdit called inputLine, where I write text and send it to QTextEdit via .append(inputLine.text()) on Return.

I want each new line (the <p> element) to appear on the bottom, and each new line being appended below first one.

This is how it looks now:

Text not aligned to bottom

I tried setStylesheet() with p { vertical-align: bottom; }.
I tried p { position: absolute; bottom: 0px; }
I tried setAlignment() with Qt.AlignBottom.

How can I make lines align to the bottom like this?

Desired alignment

Upvotes: 2

Views: 3926

Answers (1)

ekhumoro
ekhumoro

Reputation: 120778

If the chat-view is going to be read-only, you don't really need a text-edit. Just use a QLabel, and then setAligment will work as expected:

self.chatView = QtGui.QLabel(self)
self.chatView..setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom)

To append text, you can do something like:

self.chatView.setText(
    '%s<p>%s</p>'
    % (self.chatView.text(), self.inputLine.text()))

(PS: you may also need to put the label in a QScrollArea).

Upvotes: 5

Related Questions