QTextdocument: Justified HTML alignment not working
-
Hey everyone!
I am using PySide6 (version 6.3.2) in Python to generate a QTextDocument widget, filled with some text, where I want to justify the alignment. This is a simplified version of my code:import sys from PySide6.QtCore import * from PySide6.QtGui import * from PySide6.QtWidgets import * class MainWindow(QMainWindow): def __init__(self): super().__init__() centralWidget = TextDocument() self.setCentralWidget(centralWidget) self.setGeometry(500, 250, 400, 300) self.show() class TextDocument(QWidget): def __init__(self, alignment: str = "justify"): super().__init__() text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." self.text = f""" <p style="text-align: {alignment};">{text}</p> """ def paintEvent(self, event: QPaintEvent) -> None: p = QPainter(self) rect = self.rect() doc = QTextDocument() doc.setHtml(self.text) doc.setTextWidth(rect.width()) doc.drawContents(p, rect) if __name__ == "__main__": app = QCoreApplication.instance() if app is None: app = QApplication(sys.argv) app.setStyle("Fusion") win = MainWindow() sys.exit(app.exec())
and this is what I get:
So the alignment is not justified as in this example here:
However, if i do the following code adjustments:
def __init__(self, alignment: str = "right"): ...
then I receive this:
So basically, left, right and center alignments work, but the justify alignment doesn't. Where is the error? Is there maybe any other way to display multiple lines of text in a justified alignment (doesn't need to be the way I have been trying).
-
Hey everyone!
I am using PySide6 (version 6.3.2) in Python to generate a QTextDocument widget, filled with some text, where I want to justify the alignment. This is a simplified version of my code:import sys from PySide6.QtCore import * from PySide6.QtGui import * from PySide6.QtWidgets import * class MainWindow(QMainWindow): def __init__(self): super().__init__() centralWidget = TextDocument() self.setCentralWidget(centralWidget) self.setGeometry(500, 250, 400, 300) self.show() class TextDocument(QWidget): def __init__(self, alignment: str = "justify"): super().__init__() text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." self.text = f""" <p style="text-align: {alignment};">{text}</p> """ def paintEvent(self, event: QPaintEvent) -> None: p = QPainter(self) rect = self.rect() doc = QTextDocument() doc.setHtml(self.text) doc.setTextWidth(rect.width()) doc.drawContents(p, rect) if __name__ == "__main__": app = QCoreApplication.instance() if app is None: app = QApplication(sys.argv) app.setStyle("Fusion") win = MainWindow() sys.exit(app.exec())
and this is what I get:
So the alignment is not justified as in this example here:
However, if i do the following code adjustments:
def __init__(self, alignment: str = "right"): ...
then I receive this:
So basically, left, right and center alignments work, but the justify alignment doesn't. Where is the error? Is there maybe any other way to display multiple lines of text in a justified alignment (doesn't need to be the way I have been trying).
@ghoang
Hello and welcome.Qt's "QSS" only supports a subset of what CSS/HTML understands. The list of "properties" is at https://doc.qt.io/qt-6/stylesheet-reference.html#list-of-properties. For text-align (even ignoring the docs say "This property is currently supported only by QPushButton and QProgressBar.") it says the Alignment can be
{ top | bottom | left | right | center }*
So apparently no
justify
.The only place I see such a constant is at https://doc.qt.io/qt-6/qt.html#AlignmentFlag-enum,
Qt::AlignJustify
. That might be used in e.g.void QTextBlockFormat::setAlignment(Qt::Alignment alignment)
, so formatting yourQTextDocument
that way in the first place. Maybe Qt can do the justification work there in the document but not when a<p style="text-align: justify">
is output?I see that QTextEdit::setAlignment(Qt::Alignment a) supports
Qt::AlignJustify
and that can edit aQTextDocument
, maybe play with that or see what happens in code for it?