在显示Markdown时设置QTEXTEDIT中的选项卡宽度?
我正在使用QTEXTEDIT
进行Markdown编辑器/查看器进行测试时,我注意到当我列出列表时,它会自动为子弹的开始增加一个凹痕(在这里)。然后,当我添加子列表时,它添加了一些很大的差距,这些差距我只能将其视为整个标签宽度。
我尝试了:
- 阅读 docs ,但是它是似乎他们没有讨论
QTEXTEDIT
编辑器中的文本,并且在子弹开头时看起来不错,这使我认为这是一个相关的问题setmarkdown
方法。QTEXTEDIT
编辑器中的文字:
- bullet 1
- sub bullet 1
- bullet 2
- sub bullet 2
- 我忘了提到,我已经阅读了如何更改qplaintextedit中的选项卡的宽度,这表明我应该使用
settabstopdistance()
。您可以在我已经尝试过的问题的代码上看到我已经尝试过的问题,但它仅在qtextedit
编辑器而不是在Markdown Viewer中起作用。 之前
我已经提供了在使用settabstopdistance()
强>问题:
- 是什么原因导致子弹开始时的凹痕?
- 是否有可能在子弹开始时清除缩进?
- 是否可以使子弹的间隙较小?
代码我用来复制该问题:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class SidebarTextEdit(QTextEdit):
def __init__(self):
super().__init__()
self.setTabStopDistance(QFontMetricsF(self.font()).horizontalAdvance(' ') * 4)
class SidebarWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# -- main layout -- #
self.main_layout = QVBoxLayout()
self.setLayout(self.main_layout)
# -- text edit -- #
self.markdown_editor = SidebarTextEdit()
self.markdown_editor.textChanged.connect(self.reload_markdown)
self.markdown_viewer = SidebarTextEdit()
self.markdown_viewer.setReadOnly(True)
self.main_layout.addWidget(self.markdown_editor)
self.main_layout.addWidget(self.markdown_viewer)
def reload_markdown(self):
markdown_text = self.markdown_editor.toPlainText()
# print(markdown_text)
self.markdown_viewer.setMarkdown(markdown_text)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = SidebarWindow()
w.show()
sys.exit(app.exec_())
I'm making a markdown editor/viewer using QTextEdit
and while testing it, I noticed that when I'm making a list, it automatically adds an indentation to the start of the bullet (picture here). Then when I'm adding sub lists, it adds some large gaps which I can only think of as a whole tab width.
I Tried:
- Reading the docs, but it seems like they didn't discuss about this
- recovering the text inside the
QTextEdit
editor and it looks just fine without the indentations at the start of the bullet, which makes me think that it is an issue related in thesetMarkdown
method.
Text inside theQTextEdit
editor:
- bullet 1
- sub bullet 1
- bullet 2
- sub bullet 2
- I forgot to mention that, I've already read about How to change the width of tabs in a QPlainTextEdit, which suggests that I should use
setTabStopDistance()
. You can see at the code which I used to reproduce the issue below that I've already tried this but it only worked at theQTextEdit
editor and not in the markdown viewer. I have provided the before and after of usingsetTabStopDistance()
Question:
- What causes the indentation at the start of the bullets?
- Is it possible it remove the indentation at the start of the bullets?
- Is it possible to make the gap of the sub bullets smaller?
Code I used to reproduce the issue:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class SidebarTextEdit(QTextEdit):
def __init__(self):
super().__init__()
self.setTabStopDistance(QFontMetricsF(self.font()).horizontalAdvance(' ') * 4)
class SidebarWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# -- main layout -- #
self.main_layout = QVBoxLayout()
self.setLayout(self.main_layout)
# -- text edit -- #
self.markdown_editor = SidebarTextEdit()
self.markdown_editor.textChanged.connect(self.reload_markdown)
self.markdown_viewer = SidebarTextEdit()
self.markdown_viewer.setReadOnly(True)
self.main_layout.addWidget(self.markdown_editor)
self.main_layout.addWidget(self.markdown_viewer)
def reload_markdown(self):
markdown_text = self.markdown_editor.toPlainText()
# print(markdown_text)
self.markdown_viewer.setMarkdown(markdown_text)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = SidebarWindow()
w.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列表缩进设置在 > 文本编辑的编辑,并且可以用
setIndentWidth()
:
The list indentation is set on the
document()
of the text edit, and can be changed withsetIndentWidth()
: