PyQt5 从 QTextEdit 获取字符格式

我使用 PyQt5 编写了一个小型文本编辑器。假设我在编辑器中输入了 Hello World,点击打印按钮后,我需要了解“Hello World”中每个字符的以下信息:

  1. 实际字符值(例如迭代器为 1 时为 H,迭代器为 2 时为 e...等)

  2. 字符是否为粗体

  3. 字符是否为斜体

  4. 字符的字体大小

从文档https://doc.qt.io/qtforpython/PySide2/QtGui/QTextBlock.html看来,可以迭代文本块。我不确定该怎么做以及如何提取上述信息。

from PyQt5 import QtCore, QtGui, QtWidgets


class freeTextPrint(QtWidgets.QMainWindow):

    def __init__(self):

        QtWidgets.QMainWindow.__init__(self)

        self.setupUI()

    

    def setupUI(self):


        #Render the font size label

        self.toolbar = self.addToolBar('format')

        labelFontSize = QtWidgets.QLabel('Font Size')

        font = QtGui.QFont()

        font.setPointSize(11)

        font.setBold(True)

        labelFontSize.setFont(font)

        self.toolbar.addWidget(labelFontSize)

        self.toolbar.addSeparator()


        #Font Size combo box

        self.fontSizeComboBox = QtWidgets.QComboBox(self)

        #Insert font sizes to the combo box

        sizeList = list(range(10, 31))

        for i in sizeList:

            self.fontSizeComboBox.addItem(str(i))

        self.fontSizeComboBox.currentIndexChanged.connect(self.fontSizeChanged)

        

        font.setBold(False)

        self.fontSizeComboBox.setFont(font)

        self.toolbar.addWidget(self.fontSizeComboBox)


        #A toogle button to set bold font to True or False

        self.toolbar.addSeparator()

        self.boldAction = QtWidgets.QAction(QtGui.QIcon('icons/format-text-bold.svg'), 'bold', self)

        self.boldAction.triggered.connect(self.bold)

        self.toolbar.addAction(self.boldAction)


        #A toogle button to set italic to true or false

        self.italicAction = QtWidgets.QAction(QtGui.QIcon('icons/format-text-italic.svg'),'italic', self)

        self.italicAction.triggered.connect(self.italic)

        self.toolbar.addAction(self.italicAction)


慕森王
浏览 149回答 1
1回答

吃鸡游戏

每个 QTextBlock 可以包含多个 QTextCharFormat,所以你不能使用block.charFormat.一种可能的解决方案是对所有字母的每个块循环使用 QTextCursor。请注意,QTextBlock 也作为一个迭代器,因此您可以从第一个块开始,然后使用它block.next()来获取下一个块,只要block.isValid()返回 True。&nbsp; &nbsp; def print(self):&nbsp; &nbsp; &nbsp; &nbsp; document = self.textEdit.document()&nbsp; &nbsp; &nbsp; &nbsp; block = document.firstBlock()&nbsp; &nbsp; &nbsp; &nbsp; while block.isValid():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor = QtGui.QTextCursor(block)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = block.text()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for l in range(block.length() - 1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; charFormat = cursor.charFormat()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = charFormat.font().pointSize()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if size < 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = document.defaultFont().pointSize()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{letter} Bold: {bold}, Italic: {italic}, Size: {size}'.format(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; letter = text[l],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bold = charFormat.fontWeight() > 50,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; italic = charFormat.fontItalic(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = size&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor.movePosition(cursor.Right)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block = block.next()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python