Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112384

QGraphicsTextItem: Markdown formatting reduces multiple newlines to one

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: Not Evaluated Not Evaluated
    • None
    • 6.4.0
    • GUI: Text handling
    • None
    • Windows

      1. With a QGraphicsTextItem, create multiple paragraphs separated by double spacing.
      2. Set text to markdown using QTextDocument::setMarkdown().

      Result: All double spacing is eliminated in the markdown formatted text.

      Adding more newlines between paragraphs does not alleviate the issue. All multiple spacing is reduced to single line spacing.

      Example:

      try:
          import PySide6.QtCore as QtCore
          import PySide6.QtWidgets as QtWidgets
      except ImportError:
          import PySide2.QtCore as QtCore
          import PySide2.QtWidgets as QtWidgets
      ​
      class MyTextItem(QtWidgets.QGraphicsTextItem):
          def __init__(self, text, parent=None):
              super(MyTextItem, self).__init__(parent)
              self.isPlain = True
              self.itemText = text
              self.setPlainText(text)
      ​
          def toggleMarkdown(self):
              if not self.isPlain:
                  self.setPlainText(self.itemText)
              else:
                  d = self.document()
                  d.setMarkdown(self.itemText)
              self.isPlain = not self.isPlain
      ​
      class Main(QtWidgets.QWidget):
          def __init__(self, parent=None):
              super(Main, self).__init__()
              w = QtWidgets.QGraphicsView()
              s = QtWidgets.QGraphicsScene()
              myItem = MyTextItem('This is a paragraph.\n\nThis is another paragraph.')
              myItem.setPos(0, 30)
              s.addItem(myItem)
              w.setScene(s)
              self.toggleButton = QtWidgets.QPushButton("Toggle view mode")
              self.toggleButton.clicked.connect(myItem.toggleMarkdown)
              self.layout = QtWidgets.QVBoxLayout()
              self.layout.addWidget(self.toggleButton)
              self.layout.addWidget(w)
              self.setLayout(self.layout)
              self.resize(300, 200)
      ​
      if __name__ == '__main__':
          app = QtWidgets.QApplication()
          win = Main()
          win.show()
          app.exec_()

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            esabraha Eskil Abrahamsen Blomfeldt
            karen.tam@autodesk.com Karen Tam
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes