如何在PyQt中设置QTextCharFormat的属性?

时间:2018-01-24 23:47:50

标签: python qt pyqt pyqt5

我正在尝试设置插入QTextEdit的图片的自定义属性。我有以下示例代码,然后将属性的值输出到终端:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class TestEditor(QWidget):

    def __init__(self):
        QWidget.__init__(self)

        layout = QVBoxLayout()
        self.setLayout(layout)
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.textEdit = QTextEdit()
        self.layout().addWidget(self.textEdit)

        document = self.textEdit.document()
        cursor = QTextCursor(document)
        cursor.insertImage("./testimage.png")
        f = cursor.charFormat()
        print(f)

        prop_id = 0x100000 + 1
        f.setProperty(prop_id, 100)
        print(f.intProperty(prop_id))
        print('------')

        block = document.firstBlock()
        while block.length() > 0:
            print(block)
            it = block.begin()
            while not it.atEnd():
                f = it.fragment()
                fmt = f.charFormat()
                print(fmt)
                print(fmt.intProperty(prop_id))
                it += 1
            block = block.next()

class TestWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.initUi()

    def initUi(self):
        layout = QVBoxLayout()
        layout.addWidget(HextEditor())

        self.setLayout(layout)
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.setWindowTitle('button tooltip')
        self.show()

def main():
    app = QApplication(sys.argv)
    window = TestWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

该程序产生的输出为:

<PyQt5.QtGui.QTextCharFormat object at 0x107109ba8>
100
------
<PyQt5.QtGui.QTextBlock object at 0x105448318>
<PyQt5.QtGui.QTextCharFormat object at 0x107109ba8>
0

请注意,第二次获取值时,它的值为0而不是100.它甚至看起来与QTextCharFormat的实例相同。我将如何完成这样的事情?我在这里错过了一些简单的东西吗?

1 个答案:

答案 0 :(得分:1)

我通过保存插入图像的范围,选择它并使用QTextCursor.setCharFormat()保存更改来解决此问题:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class TestEditor(QWidget):

    def __init__(self):
        QWidget.__init__(self)

        layout = QVBoxLayout()
        self.setLayout(layout)
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.textEdit = QTextEdit()
        self.layout().addWidget(self.textEdit)

        document = self.textEdit.document()
        cursor = QTextCursor(document)

        # Save the position of the beginning and end of the inserted image
        p1 = cursor.position()
        cursor.insertImage("./testimage.png")
        p2 = cursor.position()

        f = cursor.charFormat()
        print(f)

        prop_id = 0x100000 + 1
        f.setProperty(prop_id, 100)

        # Select the inserted fragment and apply format
        cursor.setPosition(p1)
        cursor.setPosition(p2, QTextCursor.KeepAnchor)
        cursor.setCharFormat(f)

        print(f.intProperty(prop_id))
        print('------')

        block = document.firstBlock()
        while block.length() > 0:
            print(block)
            it = block.begin()
            while not it.atEnd():
                f = it.fragment()
                fmt = f.charFormat()
                print(fmt)
                print(fmt.intProperty(prop_id))
                it += 1
            block = block.next()

class TestWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.initUi()

    def initUi(self):
        layout = QVBoxLayout()
        layout.addWidget(TestEditor())

        self.setLayout(layout)
        self.layout().setSpacing(0)
        self.layout().setContentsMargins(0, 0, 0, 0)
        self.setWindowTitle('button tooltip')
        self.show()

def main():
    app = QApplication(sys.argv)
    window = TestWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()