|
发表于 2024-3-13 11:47:44
|
显示全部楼层
非常抱歉之前的回答没有解决你的问题。在PySide6中,QTextEdit控件的样式表并不支持直接设置行间距。不过,我们可以通过自定义QTextEdit的子类来实现设置行间距的功能。下面是一个示例代码:
- from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit
- from PySide6.QtGui import QFontMetrics, QTextCursor
- class CustomTextEdit(QTextEdit):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setStyleSheet("font-family:'隶书'; font-size:17pt;") # 设置字体和字号
-
- def setLineSpacing(self, spacing):
- fm = QFontMetrics(self.currentFont())
- self.setFixedHeight(fm.lineSpacing() + spacing) # 设置固定高度
-
- def appendText(self, text):
- cursor = self.textCursor()
- cursor.movePosition(QTextCursor.End)
- cursor.insertText(text)
- self.setTextCursor(cursor)
- app = QApplication([])
- window = QMainWindow()
- text_edit = CustomTextEdit(window)
- text_edit.setLineSpacing(6) # 设置行间距为6像素
- text_edit.appendText("该目标文件夹下共有 5 个工作簿。分别是:")
- window.setCentralWidget(text_edit)
- window.show()
- app.exec()
复制代码
在上述代码中,我们自定义了一个名为CustomTextEdit的子类,重写了setLineSpacing方法和appendText方法。setLineSpacing方法用于设置行间距,通过调整QTextEdit的固定高度实现。appendText方法用于追加文本内容,并保持滚动到文本的末尾。
使用CustomTextEdit替代原来的QTextEdit,并调用setLineSpacing方法设置行间距,应该可以达到你想要的效果。希望这次的回答对您有帮助!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|