Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Line spacing in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Line spacing in QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    Devik
    wrote on last edited by
    #1

    Is it possible to make line spacing for QTextEdit?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are looking for the QTextBlockFormat class.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You are looking for the QTextBlockFormat class.

        D Offline
        D Offline
        Devik
        wrote on last edited by
        #3

        @SGaist said in Line spacing in QTextEdit:

        Hi,

        You are looking for the QTextBlockFormat class.

        I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

        M 1 Reply Last reply
        0
        • D Devik

          @SGaist said in Line spacing in QTextEdit:

          Hi,

          You are looking for the QTextBlockFormat class.

          I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Devik said in Line spacing in QTextEdit:

          I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

          Look at the following link:
          https://stackoverflow.com/questions/10250533/set-line-spacing-in-qtextedit/66091502#66091502

          D 1 Reply Last reply
          1
          • M mpergand

            @Devik said in Line spacing in QTextEdit:

            I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

            Look at the following link:
            https://stackoverflow.com/questions/10250533/set-line-spacing-in-qtextedit/66091502#66091502

            D Offline
            D Offline
            Devik
            wrote on last edited by
            #5

            @mpergand said in Line spacing in QTextEdit:

            @Devik said in Line spacing in QTextEdit:

            I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

            Look at the following link:
            https://stackoverflow.com/questions/10250533/set-line-spacing-in-qtextedit/66091502#66091502

            What am I doing wrong?

            ui_untitled

            from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
                QMetaObject, QObject, QPoint, QRect,
                QSize, QTime, QUrl, Qt)
            from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
                QFont, QFontDatabase, QGradient, QIcon,
                QImage, QKeySequence, QLinearGradient, QPainter,
                QPalette, QPixmap, QRadialGradient, QTransform)
            from PySide6.QtWidgets import (QApplication, QMainWindow, QSizePolicy, QTextEdit,
                QVBoxLayout, QWidget)
            
            class Ui_MainWindow(object):
                def setupUi(self, MainWindow):
                    if not MainWindow.objectName():
                        MainWindow.setObjectName(u"MainWindow")
                    MainWindow.resize(363, 208)
                    self.centralwidget = QWidget(MainWindow)
                    self.centralwidget.setObjectName(u"centralwidget")
                    self.verticalLayout = QVBoxLayout(self.centralwidget)
                    self.verticalLayout.setObjectName(u"verticalLayout")
                    self.textEdit = QTextEdit(self.centralwidget)
                    self.textEdit.setObjectName(u"textEdit")
                    font = QFont()
                    font.setPointSize(14)
                    self.textEdit.setFont(font)
            
                    self.verticalLayout.addWidget(self.textEdit)
            
                    MainWindow.setCentralWidget(self.centralwidget)
            
                    self.retranslateUi(MainWindow)
            
                    QMetaObject.connectSlotsByName(MainWindow)
                # setupUi
            
                def retranslateUi(self, MainWindow):
                    MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
                # retranslateUi
            

            main.py

            from ui_untitled import *
            from PySide6.QtGui import QTextBlockFormat
            from sys import argv, exit
            
            
            class Test(QMainWindow, Ui_MainWindow):
                def __init__(self):
                    super().__init__()
                    self.setupUi(self)
            
                    self.textEdit.setText('Sample text Sample text Sample text Sample text Sample text Sample text Sample text '
                                          'Sample text Sample text Sample text Sample text Sample text Sample text Sample text')
            
                    bf = self.textEdit.textCursor().blockFormat()
                    bf.setLineHeight(50, QTextBlockFormat.LineDistanceHeight)
                    bf.setLineHeight(lineSpacing, QTextBlockFormat.LineDistanceHeight)
                    self.textEdit.textCursor().setBlockFormat(bf)
            
            
            if __name__ == "__main__":
                app = QApplication(argv)
                window = Test()
                window.show()
            
                exit(app.exec())
            
            JonBJ 1 Reply Last reply
            0
            • D Devik

              @mpergand said in Line spacing in QTextEdit:

              @Devik said in Line spacing in QTextEdit:

              I found the setLineHeight method, but I don't understand how I can apply it to my TextEdit. Can you show me an example?

              Look at the following link:
              https://stackoverflow.com/questions/10250533/set-line-spacing-in-qtextedit/66091502#66091502

              What am I doing wrong?

              ui_untitled

              from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
                  QMetaObject, QObject, QPoint, QRect,
                  QSize, QTime, QUrl, Qt)
              from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
                  QFont, QFontDatabase, QGradient, QIcon,
                  QImage, QKeySequence, QLinearGradient, QPainter,
                  QPalette, QPixmap, QRadialGradient, QTransform)
              from PySide6.QtWidgets import (QApplication, QMainWindow, QSizePolicy, QTextEdit,
                  QVBoxLayout, QWidget)
              
              class Ui_MainWindow(object):
                  def setupUi(self, MainWindow):
                      if not MainWindow.objectName():
                          MainWindow.setObjectName(u"MainWindow")
                      MainWindow.resize(363, 208)
                      self.centralwidget = QWidget(MainWindow)
                      self.centralwidget.setObjectName(u"centralwidget")
                      self.verticalLayout = QVBoxLayout(self.centralwidget)
                      self.verticalLayout.setObjectName(u"verticalLayout")
                      self.textEdit = QTextEdit(self.centralwidget)
                      self.textEdit.setObjectName(u"textEdit")
                      font = QFont()
                      font.setPointSize(14)
                      self.textEdit.setFont(font)
              
                      self.verticalLayout.addWidget(self.textEdit)
              
                      MainWindow.setCentralWidget(self.centralwidget)
              
                      self.retranslateUi(MainWindow)
              
                      QMetaObject.connectSlotsByName(MainWindow)
                  # setupUi
              
                  def retranslateUi(self, MainWindow):
                      MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
                  # retranslateUi
              

              main.py

              from ui_untitled import *
              from PySide6.QtGui import QTextBlockFormat
              from sys import argv, exit
              
              
              class Test(QMainWindow, Ui_MainWindow):
                  def __init__(self):
                      super().__init__()
                      self.setupUi(self)
              
                      self.textEdit.setText('Sample text Sample text Sample text Sample text Sample text Sample text Sample text '
                                            'Sample text Sample text Sample text Sample text Sample text Sample text Sample text')
              
                      bf = self.textEdit.textCursor().blockFormat()
                      bf.setLineHeight(50, QTextBlockFormat.LineDistanceHeight)
                      bf.setLineHeight(lineSpacing, QTextBlockFormat.LineDistanceHeight)
                      self.textEdit.textCursor().setBlockFormat(bf)
              
              
              if __name__ == "__main__":
                  app = QApplication(argv)
                  window = Test()
                  window.show()
              
                  exit(app.exec())
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @Devik said in Line spacing in QTextEdit:

                  bf.setLineHeight(50, QTextBlockFormat.LineDistanceHeight)
                  bf.setLineHeight(lineSpacing, QTextBlockFormat.LineDistanceHeight)
              

              If this is your actual code you should get NameError: name 'lineSpacing' is not defined error.
              If this is not your actual code then we don't know what your code really is.
              Please only post code which is actually copied & pasted from what you are really testing!

              The following works fine for me:

              import sys
              from PySide2 import QtWidgets, QtGui
              
              if __name__ == '__main__':
                  app = QtWidgets.QApplication(sys.argv)
                  textEdit = QtWidgets.QTextEdit()
                  textEdit.show()
                 
                  textEdit.setText('Sample text Sample text Sample text Sample text Sample text Sample text Sample text '
                                   'Sample text Sample text Sample text Sample text Sample text Sample text Sample text')
                  bf = textEdit.textCursor().blockFormat()
                  bf.setLineHeight(50, QtGui.QTextBlockFormat.LineDistanceHeight)
              #    bf.setLineHeight(lineSpacing, QtWidgets.QTextBlockFormat.LineDistanceHeight)
                  textEdit.textCursor().setBlockFormat(bf)
                      
                  sys.exit(app.exec_())
              

              Screenshot 2024-05-06 082538.png
              Tested under Ubuntu 22.04, PySide2, Qt5.15. That's all I have.

              1 Reply Last reply
              2

              • Login

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