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


Python QFontMetrics.elidedText方法代码示例

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


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

示例1: __textLayout

# 需要导入模块: from AnyQt.QtGui import QFontMetrics [as 别名]
# 或者: from AnyQt.QtGui.QFontMetrics import elidedText [as 别名]
    def __textLayout(self):
        fm = QFontMetrics(self.font())
        text = self.defaultAction().text()
        words = deque(text.split())

        lines = []
        curr_line = ""
        curr_line_word_count = 0

        option = QStyleOptionToolButton()
        option.initFrom(self)

        margin = self.style().pixelMetric(QStyle.PM_ButtonMargin, option, self)
        width = self.width() - 2 * margin

        while words:
            w = words.popleft()

            if curr_line_word_count:
                line_extended = " ".join([curr_line, w])
            else:
                line_extended = w

            line_w = fm.boundingRect(line_extended).width()

            if line_w >= width:
                if curr_line_word_count == 0 or len(lines) == 1:
                    # A single word that is too long must be elided.
                    # Also if the text overflows 2 lines
                    # Warning: hardcoded max lines
                    curr_line = fm.elidedText(line_extended, Qt.ElideRight,
                                              width)
                    curr_line = curr_line
                else:
                    # Put the word back
                    words.appendleft(w)

                lines.append(curr_line)
                curr_line = ""
                curr_line_word_count = 0
                if len(lines) == 2:
                    break
            else:
                curr_line = line_extended
                curr_line_word_count += 1

        if curr_line:
            lines.append(curr_line)

        text = "\n".join(lines)
        text = text.replace('&', '&&')  # Need escaped ampersand to show

        self.__text = text
开发者ID:ales-erjavec,项目名称:orange-canvas,代码行数:55,代码来源:toolgrid.py

示例2: set_widget_value

# 需要导入模块: from AnyQt.QtGui import QFontMetrics [as 别名]
# 或者: from AnyQt.QtGui.QFontMetrics import elidedText [as 别名]
def set_widget_value(widget, value):
    if isinstance(widget, QLineEdit):
        widget.setText(value)
    elif isinstance(widget, QPlainTextEdit):
        return widget.setPlainText(value)
    elif isinstance(widget, QLabel):
        if widget.openExternalLinks():
            widget.setToolTip(value)
            metrics = QFontMetrics(widget.font())
            display = metrics.elidedText(value, Qt.ElideRight, widget.width())
            value = urllib.parse.unquote(value)     # Because setText() escapes percent-encoded values and corrupts them
            value = '<a href="{value}">{display}</a>'.format(**locals())
        widget.setText(value)
    elif isinstance(widget, QCheckBox):
        return widget.setChecked(value not in (False, '0', '', 'False'))
    else:
        raise RuntimeError()
开发者ID:rokups,项目名称:paste2box,代码行数:19,代码来源:common.py

示例3: paintEvent

# 需要导入模块: from AnyQt.QtGui import QFontMetrics [as 别名]
# 或者: from AnyQt.QtGui.QFontMetrics import elidedText [as 别名]
 def paintEvent(self, event):
     QLineEdit.paintEvent(self, event)
     if not self.text() and self.placeholderText() and \
             not self.hasFocus():
         p = QStylePainter(self)
         font = self.font()
         metrics = QFontMetrics(font)
         p.setFont(font)
         color = self.palette().color(QPalette.Mid)
         p.setPen(color)
         left, top, right, bottom = self.getTextMargins()
         contents = self.contentsRect()
         contents = contents.adjusted(left, top, -right, -bottom)
         text = metrics.elidedText(self.placeholderText(),
                                   Qt.ElideMiddle,
                                   contents.width())
         p.drawText(contents, Qt.AlignLeft | Qt.AlignVCenter, text)
开发者ID:PrimozGodec,项目名称:orange3,代码行数:19,代码来源:lineedit.py

示例4: set_text

# 需要导入模块: from AnyQt.QtGui import QFontMetrics [as 别名]
# 或者: from AnyQt.QtGui.QFontMetrics import elidedText [as 别名]
 def set_text(self):
     metrics = QFontMetrics(self.font())
     self.setText(metrics.elidedText(self.desc_text, Qt.ElideRight,
                                     self.width() - 15))
开发者ID:lanzagar,项目名称:orange3,代码行数:6,代码来源:owselectrows.py

示例5: __paintEventNoStyle

# 需要导入模块: from AnyQt.QtGui import QFontMetrics [as 别名]
# 或者: from AnyQt.QtGui.QFontMetrics import elidedText [as 别名]
    def __paintEventNoStyle(self):
        p = QPainter(self)
        opt = QStyleOptionToolButton()
        self.initStyleOption(opt)

        fm = QFontMetrics(opt.font)
        palette = opt.palette

        # highlight brush is used as the background for the icon and background
        # when the tab is expanded and as mouse hover color (lighter).
        brush_highlight = palette.highlight()
        foregroundrole = QPalette.ButtonText
        if opt.state & QStyle.State_Sunken:
            # State 'down' pressed during a mouse press (slightly darker).
            background_brush = brush_darker(brush_highlight, 110)
            foregroundrole = QPalette.HighlightedText
        elif opt.state & QStyle.State_MouseOver:
            background_brush = brush_darker(brush_highlight, 95)
            foregroundrole = QPalette.HighlightedText
        elif opt.state & QStyle.State_On:
            background_brush = brush_highlight
            foregroundrole = QPalette.HighlightedText
        else:
            # The default button brush.
            background_brush = palette.button()

        rect = opt.rect

        icon_area_rect = QRect(rect)
        icon_area_rect.setRight(int(icon_area_rect.height() * 1.26))

        text_rect = QRect(rect)
        text_rect.setLeft(icon_area_rect.right() + 10)

        # Background  (TODO: Should the tab button have native
        # toolbutton shape, drawn using PE_PanelButtonTool or even
        # QToolBox tab shape)

        # Default outline pen
        pen = QPen(palette.color(QPalette.Mid))

        p.save()
        p.setPen(Qt.NoPen)
        p.setBrush(QBrush(background_brush))
        p.drawRect(rect)

        # Draw the background behind the icon if the background_brush
        # is different.
        if not opt.state & QStyle.State_On:
            p.setBrush(brush_highlight)
            p.drawRect(icon_area_rect)
            # Line between the icon and text
            p.setPen(pen)
            p.drawLine(icon_area_rect.topRight(),
                       icon_area_rect.bottomRight())

        if opt.state & QStyle.State_HasFocus:
            # Set the focus frame pen and draw the border
            pen = QPen(QColor(FOCUS_OUTLINE_COLOR))
            p.setPen(pen)
            p.setBrush(Qt.NoBrush)
            # Adjust for pen
            rect = rect.adjusted(0, 0, -1, -1)
            p.drawRect(rect)

        else:
            p.setPen(pen)
            # Draw the top/bottom border
            if self.position == QStyleOptionToolBox.OnlyOneTab or \
                    self.position == QStyleOptionToolBox.Beginning or \
                    self.selected & \
                        QStyleOptionToolBox.PreviousIsSelected:

                p.drawLine(rect.topLeft(), rect.topRight())

            p.drawLine(rect.bottomLeft(), rect.bottomRight())

        p.restore()

        p.save()
        text = fm.elidedText(opt.text, Qt.ElideRight, text_rect.width())
        p.setPen(QPen(palette.color(foregroundrole)))
        p.setFont(opt.font)

        p.drawText(text_rect,
                   int(Qt.AlignVCenter | Qt.AlignLeft) | \
                   int(Qt.TextSingleLine),
                   text)

        if not opt.icon.isNull():
            if opt.state & QStyle.State_Enabled:
                mode = QIcon.Normal
            else:
                mode = QIcon.Disabled
            if opt.state & QStyle.State_On:
                state = QIcon.On
            else:
                state = QIcon.Off
            icon_area_rect = icon_area_rect
            icon_rect = QRect(QPoint(0, 0), opt.iconSize)
#.........这里部分代码省略.........
开发者ID:ales-erjavec,项目名称:orange-canvas,代码行数:103,代码来源:toolbox.py


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