显示 Markdown 时在 QTextEdit 中设置制表符宽度?

Setting tab width in QTextEdit when displaying Markdown?

我正在使用 QTextEdit 进行降价 editor/viewer 并且在测试它时,我注意到当我制作列表时,它会自动在项目符号的开头添加缩进 ( picture here)。然后当我添加子列表时,它会添加一些大的间隙,我只能将其视为整个标签宽度。

我试过了:

  1. 正在阅读docs,但他们似乎没有讨论过这个
  2. 恢复 QTextEdit 编辑器中的文本,它看起来很好,没有项目符号开头的缩进,这让我认为这是与 setMarkdown 方法相关的问题。 QTextEdit 编辑器中的文本:
    - bullet 1
       - sub bullet 1
    - bullet 2
       - sub bullet 2
  1. 我忘了说了,我已经读过 , 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 the QTextEdit editor and not in the markdown viewer. I have provided the before and after 使用 setTabStopDistance()

问题:

  1. 项目符号开头缩进的原因是什么?
  2. 是否可以删除项目符号开头的缩进?
  3. 能不能把子弹的间隙做小点?

我用来重现问题的代码:

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_())

列表缩进设置在document() of the text edit, and can be changed with setIndentWidth():

class SidebarTextEdit(QTextEdit):
    def __init__(self):
        super().__init__()
        indent = QFontMetricsF(self.font()).horizontalAdvance('    ')
        self.document().setIndentWidth(indent)