本文整理汇总了Python中PyQt5.Qt.QFontMetrics.width方法的典型用法代码示例。如果您正苦于以下问题:Python QFontMetrics.width方法的具体用法?Python QFontMetrics.width怎么用?Python QFontMetrics.width使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.Qt.QFontMetrics
的用法示例。
在下文中一共展示了QFontMetrics.width方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: formatText
# 需要导入模块: from PyQt5.Qt import QFontMetrics [as 别名]
# 或者: from PyQt5.Qt.QFontMetrics import width [as 别名]
def formatText(self, font):
metrics = QFontMetrics(font)
width = metrics.width(self.text)
need_to_modify = False
while(width > Words.MAXIMUM_TEXT_WIDTH):
need_to_modify = True
self.text = self.text[0:-1]
width = metrics.width(self.text)
if need_to_modify:
self.text = self.text[0:-3]
self.text = self.text + "..."
示例2: populate
# 需要导入模块: from PyQt5.Qt import QFontMetrics [as 别名]
# 或者: from PyQt5.Qt.QFontMetrics import width [as 别名]
def populate(self, phrase, ts, process_space=True):
phrase_pos = 0
processed = False
matches = self.__class__.whitespace.finditer(phrase)
font = QFont(ts.font)
if self.valign is not None:
font.setPixelSize(font.pixelSize()/1.5)
fm = QFontMetrics(font)
single_space_width = fm.width(' ')
height, descent = fm.height(), fm.descent()
for match in matches:
processed = True
left, right = match.span()
if not process_space:
right = left
space_width = single_space_width * (right-left)
word = phrase[phrase_pos:left]
width = fm.width(word)
if self.current_width + width < self.line_length:
self.commit(word, width, height, descent, ts, font)
if space_width > 0 and self.current_width + space_width < self.line_length:
self.add_space(space_width)
phrase_pos = right
continue
# Word doesn't fit on line
if self.hyphenate and len(word) > 3:
tokens = hyphenate_word(word)
for i in range(len(tokens)-2, -1, -1):
word = ''.join(tokens[0:i+1])+'-'
width = fm.width(word)
if self.current_width + width < self.line_length:
self.commit(word, width, height, descent, ts, font)
return phrase_pos + len(word)-1, True
if self.current_width < 5: # Force hyphenation as word is longer than line
for i in range(len(word)-5, 0, -5):
part = word[:i] + '-'
width = fm.width(part)
if self.current_width + width < self.line_length:
self.commit(part, width, height, descent, ts, font)
return phrase_pos + len(part)-1, True
# Failed to add word.
return phrase_pos, True
if not processed:
return self.populate(phrase+' ', ts, False)
return phrase_pos, False
示例3: do_size_hint
# 需要导入模块: from PyQt5.Qt import QFontMetrics [as 别名]
# 或者: from PyQt5.Qt.QFontMetrics import width [as 别名]
def do_size_hint(self, option, index):
text = index.data(Qt.DisplayRole) or ''
font = QFont(option.font)
font.setPointSize(QFontInfo(font).pointSize() * 1.5)
m = QFontMetrics(font)
return QSize(m.width(text), m.height())