PyQt: QTextEdit 设置 QTextCharFormat 第一个块输入时块字体格式无效,将光标移动到块开头即可PyQt: QTextEdit 设置 QTextCharFormat 第一个块输入时块字体格式无效,将光标移动到块开头即可PyQt: QTextEdit 设置 QTextCharFormat 第一个块输入时块字体格式无效,将光标移动到块开头即可PyQt: QTextEdit 设置 QTextCharFormat 第一个块输入时块字体格式无效,将光标移动到块开头即可
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

PyQt: QTextEdit 设置 QTextCharFormat 第一个块输入时块字体格式无效,将光标移动到块开头即可

发表 admin at 2022年4月8日
类别
  • Practice
标签
import sys
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.user_action()

    def setup_ui(self):
        self.setWindowTitle("Qt桌面应用程序")
        self.resize(300, 300)

        self.push_button = QPushButton(self)
        self.push_button.setText("按钮")
        self.push_button.move(50, 10)

        self.text_edit = QTextEdit(self)
        self.text_edit.resize(200, 200)
        self.text_edit.move(50, 50)

    def user_action(self):
        def push_button_clicked():
            text_edit_text_cursor = self.text_edit.textCursor()

            text_edit_text_cursor_text_char_format = QTextCharFormat()
            text_edit_text_cursor_text_char_format.setFontFamily("幼圆")
            text_edit_text_cursor_text_char_format.setFontPointSize(30)
            text_edit_text_cursor.setCharFormat(text_edit_text_cursor_text_char_format)
            text_edit_text_cursor.setBlockCharFormat(text_edit_text_cursor_text_char_format)

            text_edit_text_cursor_text_block_format = QTextBlockFormat()
            text_edit_text_cursor_text_block_format.setTextIndent(20)
            text_edit_text_cursor.setBlockFormat(text_edit_text_cursor_text_block_format)

            #第一个块输入时块字体格式无效,将光标移动到块开头即可
            text_edit_text_cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor, 1)
            self.text_edit.setTextCursor(text_edit_text_cursor)

            self.text_edit.setFocus()

        self.push_button.clicked.connect(push_button_clicked)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

类别

  • Cat
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader