Line spacing in QTextEdit
-
Hi,
You are looking for the QTextBlockFormat class.
-
@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?
-
@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 -
@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#66091502What 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
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())
-
@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_())
Tested under Ubuntu 22.04, PySide2, Qt5.15. That's all I have.