简体   繁体   中英

How do I return text of specific font size and color from QTextDocument?

I have a QTextEdit which holds some formatted text. How do I return all text that matches a specific font size and/or font color?

I have tried the QTextDocument.objectForFormat() method, as an argument I provided a QTextFormat object with the property FontPointSize, but that returns None .

import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QApplication
from PyQt5.QtGui import QColor
from PyQt5 import Qt
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.textEdit.setTextColor(QColor('#00aaff'))
        self.textEdit.setFontPointSize(14.0)
        self.textEdit.insertPlainText('Title\n')
        self.textEdit.setTextColor(QColor('#e0e0e0'))
        self.textEdit.setFontPointSize(11.0)
        self.textEdit.insertPlainText('content\n')
        self.textEdit.setTextColor(QColor('#00aaff'))
        self.textEdit.setFontPointSize(14.0)
        self.textEdit.insertPlainText('Title2\n')
        self.textEdit.setTextColor(QColor('#e0e0e0'))
        self.textEdit.setFontPointSize(11.0)
        self.textEdit.insertPlainText('content_title2')
        self.printFontSizes()
        self.show()

    def printFontSizes(self):
        doc = self.textEdit.document()
        for i in range(doc.blockCount()):
            print(doc.findBlockByNumber(i).text(),': ',
            doc.findBlockByNumber(i).charFormat().fontPointSize())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())

Running the above code prints this:

Title :  0.0
content :  14.0
Title2 :  11.0
content_title2 :  14.0

Is there something I am doing wrong here?

Since in my particular example the above code was displaying the wrong font size, I instead solved the problem by iterating over each line in the QTextEdit using QTextCursor and checking each one's font size. Here's the relevant code:

#!/bin/python3
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QApplication
from PyQt5.QtGui import QColor, QTextCursor
from PyQt5 import Qt
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.textEdit = QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.textEdit.setTextColor(QColor('#00aaff'))
        self.textEdit.setFontPointSize(14.0)
        self.textEdit.insertPlainText('Title\n')
        self.textEdit.setTextColor(QColor('#e0e0e0'))
        self.textEdit.setFontPointSize(11.0)
        self.textEdit.insertPlainText('content\n')
        self.textEdit.setTextColor(QColor('#00aaff'))
        self.textEdit.setFontPointSize(14.0)
        self.textEdit.insertPlainText('Title2\n')
        self.textEdit.setTextColor(QColor('#e0e0e0'))
        self.textEdit.setFontPointSize(11.0)
        self.textEdit.insertPlainText('content_title2\n')
        self.textEdit.insertPlainText('content2_title2\n')
        self.printFontSizes()
        self.show()

    def printFontSizes(self):
        doc = self.textEdit.document()
        self.textEdit.moveCursor(QTextCursor.Start)
        for i in range(doc.blockCount()):
            if self.textEdit.fontPointSize() == 14.0:
                print(doc.findBlockByNumber(i).text())
            self.textEdit.moveCursor(QTextCursor.NextBlock)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM