Skip to content

Commit

Permalink
支持显示聊天图片
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 committed Nov 21, 2023
1 parent edbe83f commit 55425da
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 80 deletions.
133 changes: 79 additions & 54 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions app/components/bubble_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,35 @@ def run(self) -> None:


class ImageMessage(QLabel):
def __init__(self, image, image_link='', max_width=480, max_height=720, parent=None):
def __init__(self, image, image_link='', max_width=480, max_height=240, parent=None):
"""
param:image 图像路径或者QPixmap对象
param:image_link='' 点击图像打开的文件路径
"""
super().__init__(parent)
self.image = QLabel(self)

self.max_width = max_width
self.max_height = max_height
if isinstance(image, str):
self.setPixmap(QPixmap(image))
pixmap = QPixmap(image)
self.image_path = image
elif isinstance(image, QPixmap):
self.setPixmap(image)
pixmap = image
self.set_image(pixmap)
if image_link:
self.image_path = image_link
self.setMaximumWidth(max_width)
self.setMaximumHeight(max_height)
self.setMaximumWidth(self.max_width)
self.setMaximumHeight(self.max_height)
# self.setScaledContents(True)

def set_image(self, pixmap):
# 计算调整后的大小
adjusted_width = min(pixmap.width(), self.max_width)
adjusted_height = min(pixmap.height(), self.max_height)
self.setPixmap(pixmap.scaled(adjusted_width, adjusted_height, Qt.KeepAspectRatio))
# 调整QLabel的大小以适应图片的宽高,但不超过最大宽高
self.setFixedSize(adjusted_width, adjusted_height)

def mousePressEvent(self, event):
if event.buttons() == Qt.LeftButton: # 左键按下
print('打开图像', self.image_path)
Expand Down
Loading

0 comments on commit 55425da

Please sign in to comment.