Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTextdocument: Justified HTML alignment not working
Forum Updated to NodeBB v4.3 + New Features

QTextdocument: Justified HTML alignment not working

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 464 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ghoang
    wrote on last edited by
    #1

    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:

    Bildschirmfoto 2022-10-03 um 13.20.52.png

    So the alignment is not justified as in this example here:

    Bildschirmfoto 2022-10-03 um 13.23.23.png

    However, if i do the following code adjustments:

    def __init__(self, alignment: str = "right"):
        ...
    

    then I receive this:

    Bildschirmfoto 2022-10-03 um 13.26.39.png

    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).

    JonBJ 1 Reply Last reply
    0
    • G ghoang

      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:

      Bildschirmfoto 2022-10-03 um 13.20.52.png

      So the alignment is not justified as in this example here:

      Bildschirmfoto 2022-10-03 um 13.23.23.png

      However, if i do the following code adjustments:

      def __init__(self, alignment: str = "right"):
          ...
      

      then I receive this:

      Bildschirmfoto 2022-10-03 um 13.26.39.png

      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).

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 your QTextDocument 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 a QTextDocument, maybe play with that or see what happens in code for it?

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved