Skip to content

Commit

Permalink
fix: fix MLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
biubiubiu533 authored and muyr committed Aug 3, 2024
1 parent 808a86f commit 9c08eab
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions dayu_widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from __future__ import division
from __future__ import print_function

# Import built-in modules
import re

# Import third-party modules
from Qt import QtCore
from Qt import QtWidgets
Expand Down Expand Up @@ -132,15 +135,15 @@ def text(self):
:returns: The original unmodified text
"""
return self.property("text")
return self.property("dayu_text")

def setText(self, text):
"""
Overridden base method to set the text on the label
:param text: The text to set on the label
"""
self.setProperty("text", text)
self.setProperty("dayu_text", text)
self._update_elided_text()
self.setToolTip(text)

Expand All @@ -160,10 +163,18 @@ def _update_elided_text(self):
Update the elided text on the label
"""
_font_metrics = self.fontMetrics()
text = self.property("text")
text = self.property("dayu_text")
text = text if text else ""
_elided_text = _font_metrics.elidedText(text, self._elide_mode, self.width() - 2 * 2)
super(MLabel, self).setText(_elided_text)
# 检查文本是否包含 HTML 标签
is_html = bool(re.search(r"<[^>]+>", text))

if is_html:
# 如果文本包含 HTML 标签,直接设置富文本
super(MLabel, self).setText(text)
else:
# 否则,使用省略模式设置文本
_elided_text = _font_metrics.elidedText(text, self._elide_mode, self.width() - 2 * 2)
super(MLabel, self).setText(_elided_text)

def resizeEvent(self, event):
"""
Expand Down

0 comments on commit 9c08eab

Please sign in to comment.