当前位置: 首页>>代码示例>>Python>>正文


Python QTextCharFormat.font方法代码示例

本文整理汇总了Python中PyQt5.QtGui.QTextCharFormat.font方法的典型用法代码示例。如果您正苦于以下问题:Python QTextCharFormat.font方法的具体用法?Python QTextCharFormat.font怎么用?Python QTextCharFormat.font使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.QtGui.QTextCharFormat的用法示例。


在下文中一共展示了QTextCharFormat.font方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SuggestionsWidget

# 需要导入模块: from PyQt5.QtGui import QTextCharFormat [as 别名]
# 或者: from PyQt5.QtGui.QTextCharFormat import font [as 别名]
class SuggestionsWidget(QWidget, Ui_SuggestionsWidget):

    STYLE_TRANSLATION, STYLE_STROKES = range(2)

    # Anatomy of the text document:
    # - "root":
    #  - 0+ "suggestions" blocks
    #   - 1+ "translation" blocks
    #    - 1-10 "strokes" blocks

    def __init__(self):
        super(SuggestionsWidget, self).__init__()
        self.setupUi(self)
        self._translation_char_format = QTextCharFormat()
        self._strokes_char_format = QTextCharFormat()
        self._strokes_char_format.font().setStyleHint(QFont.Monospace)

    def prepend(self, suggestion_list):
        before_height = self.suggestions.document().size().height()
        cursor = self.suggestions.textCursor()
        cursor.movePosition(QTextCursor.Start)
        for suggestion in suggestion_list:
            cursor.insertBlock()
            cursor.setCharFormat(self._translation_char_format)
            cursor.block().setUserState(self.STYLE_TRANSLATION)
            cursor.insertText(escape_translation(suggestion.text) + u':')
            if not suggestion.steno_list:
                cursor.insertText(u' ' + _('no suggestions'))
                continue
            for strokes_list in suggestion.steno_list[:10]:
                cursor.insertBlock()
                cursor.setCharFormat(self._strokes_char_format)
                cursor.block().setUserState(self.STYLE_STROKES)
                cursor.insertText(u'   ' + u'/'.join(strokes_list))
        cursor.insertText('\n')
        # Keep current position when not at the top of the document.
        scrollbar_value = self.suggestions.verticalScrollBar().value()
        if scrollbar_value != 0:
            after_height = self.suggestions.document().size().height()
            delta_height = after_height - before_height
            self.suggestions.verticalScrollBar().setValue(scrollbar_value + delta_height)

    def clear(self):
        self.suggestions.clear()

    def _reformat(self):
        document = self.suggestions.document()
        cursor = self.suggestions.textCursor()
        block = document.begin()
        style_format = {
            self.STYLE_TRANSLATION: self._translation_char_format,
            self.STYLE_STROKES: self._strokes_char_format,
        }
        while block != document.end():
            style = block.userState()
            fmt = style_format.get(style)
            if fmt is not None:
                cursor.setPosition(block.position())
                cursor.select(QTextCursor.BlockUnderCursor)
                cursor.setCharFormat(fmt)
            block = block.next()

    @property
    def text_font(self):
        return self._translation_char_format.font()

    @text_font.setter
    def text_font(self, font):
        self._translation_char_format.setFont(font)
        self._reformat()

    @property
    def strokes_font(self):
        return self._strokes_char_format.font()

    @strokes_font.setter
    def strokes_font(self, font):
        self._strokes_char_format.setFont(font)
        self._reformat()
开发者ID:morinted,项目名称:plover,代码行数:81,代码来源:suggestions_widget.py


注:本文中的PyQt5.QtGui.QTextCharFormat.font方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。