聊天记录从后往前显示

This commit is contained in:
shuaikangzhou 2023-11-18 11:51:58 +08:00
parent fd8d0ed153
commit c72b67f2a0
6 changed files with 70 additions and 52 deletions

View File

@ -4,8 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="84e65474-7da9-466d-baf3-cc88dde3ffdd" name="变更" comment="将自己的wxid设置为配置文件"> <list default="true" id="84e65474-7da9-466d-baf3-cc88dde3ffdd" name="变更" comment="修改聊天记录显示顺序">
<change beforePath="$PROJECT_DIR$/app/DataBase/msg.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/DataBase/msg.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/DataBase/micro_msg.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/DataBase/micro_msg.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/components/bubble_message.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/components/bubble_message.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/chat/chat_info.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/chat/chat_info.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/chat/chat_window.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/chat/chat_window.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app/ui_pc/contact/contact_window.py" beforeDir="false" afterPath="$PROJECT_DIR$/app/ui_pc/contact/contact_window.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -252,8 +257,8 @@
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Python.main_pc" /> <item itemvalue="Python.main_pc" />
<item itemvalue="Python.person" />
<item itemvalue="Python.bubble_message" /> <item itemvalue="Python.bubble_message" />
<item itemvalue="Python.person" />
<item itemvalue="Python.chat_info" /> <item itemvalue="Python.chat_info" />
<item itemvalue="Python.msg" /> <item itemvalue="Python.msg" />
</list> </list>
@ -271,13 +276,6 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1672848140146</updated> <updated>1672848140146</updated>
</task> </task>
<task id="LOCAL-00023" summary="readme">
<created>1684599566617</created>
<option name="number" value="00023" />
<option name="presentableId" value="LOCAL-00023" />
<option name="project" value="LOCAL" />
<updated>1684599566617</updated>
</task>
<task id="LOCAL-00024" summary="readme"> <task id="LOCAL-00024" summary="readme">
<created>1684599688133</created> <created>1684599688133</created>
<option name="number" value="00024" /> <option name="number" value="00024" />
@ -614,7 +612,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1700236486745</updated> <updated>1700236486745</updated>
</task> </task>
<option name="localTasksCounter" value="72" /> <task id="LOCAL-00072" summary="修改聊天记录显示顺序">
<created>1700236795662</created>
<option name="number" value="00072" />
<option name="presentableId" value="LOCAL-00072" />
<option name="project" value="LOCAL" />
<updated>1700236795662</updated>
</task>
<option name="localTasksCounter" value="73" />
<servers /> <servers />
</component> </component>
<component name="UnknownFeatures"> <component name="UnknownFeatures">
@ -650,7 +655,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="加快打开速度" />
<MESSAGE value="修改导入路径方便打包成exe" /> <MESSAGE value="修改导入路径方便打包成exe" />
<MESSAGE value="修改部分UI" /> <MESSAGE value="修改部分UI" />
<MESSAGE value="上传html模板" /> <MESSAGE value="上传html模板" />
@ -675,7 +679,8 @@
<MESSAGE value="修复聊天气泡不能更改大小的bug" /> <MESSAGE value="修复聊天气泡不能更改大小的bug" />
<MESSAGE value="更改头像显示" /> <MESSAGE value="更改头像显示" />
<MESSAGE value="将自己的wxid设置为配置文件" /> <MESSAGE value="将自己的wxid设置为配置文件" />
<option name="LAST_COMMIT_MESSAGE" value="将自己的wxid设置为配置文件" /> <MESSAGE value="修改聊天记录显示顺序" />
<option name="LAST_COMMIT_MESSAGE" value="修改聊天记录显示顺序" />
<option name="OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT" value="true" /> <option name="OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT" value="true" />
<option name="REFORMAT_BEFORE_PROJECT_COMMIT" value="true" /> <option name="REFORMAT_BEFORE_PROJECT_COMMIT" value="true" />
</component> </component>

View File

@ -1,7 +1,8 @@
import os.path import os.path
import sqlite3 import sqlite3
import time import threading
lock = threading.Lock()
DB = None DB = None
cursor = None cursor = None
micromsg_path = "./app/Database/Msg/MicroMsg.db" micromsg_path = "./app/Database/Msg/MicroMsg.db"
@ -27,6 +28,7 @@ def is_database_exist():
def get_contact(): def get_contact():
try: try:
lock.acquire(True)
sql = '''select UserName,Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl.bigHeadImgUrl sql = '''select UserName,Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl.bigHeadImgUrl
from Contact inner join ContactHeadImgUrl on Contact.UserName = ContactHeadImgUrl.usrName from Contact inner join ContactHeadImgUrl on Contact.UserName = ContactHeadImgUrl.usrName
where Type=3 and Alias is not null where Type=3 and Alias is not null
@ -34,15 +36,8 @@ def get_contact():
''' '''
cursor.execute(sql) cursor.execute(sql)
result = cursor.fetchall() result = cursor.fetchall()
except: finally:
time.sleep(0.2) lock.release()
sql = '''select UserName,Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl.bigHeadImgUrl
from Contact inner join ContactHeadImgUrl on Contact.UserName = ContactHeadImgUrl.usrName
where Type=3 and Alias is not null
order by PYInitial
'''
cursor.execute(sql)
result = cursor.fetchall()
# DB.commit() # DB.commit()
return result return result

View File

@ -250,12 +250,12 @@ class ScrollBar(QScrollBar):
) )
class MyWidget(QWidget): class ChatWidget(QWidget):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.resize(500, 200) self.resize(500, 200)
txt = '''在工具中单击边缘可以添加黑点,单击可以删掉黑点,拖动可以调整黑点长度。勾选等选项可以查看内容、缩放等区域右侧可预览不同拉伸情况下的效果,拖动可以调整预览的拉伸比例''' txt = '''在工具中单击边缘可以添加黑点,单击可以删掉黑点,拖动可以调整黑点长度。勾选等选项可以查看内容、缩放等区域右侧可预览不同拉伸情况下的效果,拖动可以调整预览的拉伸比例'''
avatar = '../data/icons/default_avatar.svg' avatar = './app/data/icons/default_avatar.svg'
bubble_message = BubbleMessage(txt, avatar, Type=1, is_send=False) bubble_message = BubbleMessage(txt, avatar, Type=1, is_send=False)
layout = QVBoxLayout() layout = QVBoxLayout()
layout.setSpacing(0) layout.setSpacing(0)
@ -272,39 +272,52 @@ class MyWidget(QWidget):
# 设置滚动区域的内容部署部件为前面生成的内容部署层部件 # 设置滚动区域的内容部署部件为前面生成的内容部署层部件
self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.scrollArea.setWidget(self.scrollAreaWidgetContents)
layout.addWidget(self.scrollArea) layout.addWidget(self.scrollArea)
layout0 = QVBoxLayout() self.layout0 = QVBoxLayout()
layout0.setSpacing(0) self.layout0.setSpacing(0)
# self.layout0.addWidget(bubble_message)
# self.scrollArea.setLayout(layout0) # self.scrollArea.setLayout(layout0)
self.scrollAreaWidgetContents.setLayout(layout0) self.scrollAreaWidgetContents.setLayout(self.layout0)
time = Notice("2023-11-17 15:44") time = Notice("2023-11-17 15:44")
layout0.addWidget(time) # self.layout0.addWidget(time)
txt = "你说啥" # txt = "你说啥"
avatar_2 = '../data/icons/default_avatar.svg' # avatar_2 = '../data/icons/default_avatar.svg'
bubble_message1 = BubbleMessage(txt, avatar_2, Type=1, is_send=True) # bubble_message1 = BubbleMessage(txt, avatar_2, Type=1, is_send=True)
layout0.addWidget(bubble_message) #
layout0.addWidget(bubble_message1) # self.layout0.addWidget(bubble_message1)
#
bubble_message2 = BubbleMessage('', avatar_2, Type=1, is_send=True) # bubble_message2 = BubbleMessage('', avatar_2, Type=1, is_send=True)
layout0.addWidget(bubble_message2) # self.layout0.addWidget(bubble_message2)
txt = "我啥都没说" # txt = "我啥都没说"
avatar0 = 'Data/fg1.png' # avatar0 = 'Data/fg1.png'
bubble_message1 = BubbleMessage("D:\Project\Python\PyQt-master\QLabel\Data\\fg1.png", avatar, Type=3, bubble_message1 = BubbleMessage("D:\Project\Python\PyQt-master\QLabel\Data\\fg1.png", avatar, Type=3,
is_send=False) is_send=False)
layout0.addWidget(bubble_message1) self.layout0.addWidget(bubble_message1)
self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding) self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
layout0.addItem(self.spacerItem) self.layout0.addItem(self.spacerItem)
# layout.setStretch(0, 1) # layout.setStretch(0, 1)
self.setLayout(layout) self.setLayout(layout)
def add_message_item(self, bubble_message):
self.layout0.addWidget(bubble_message)
def set_scroll_bar_last(self):
self.scrollArea.verticalScrollBar().setValue(self.scrollArea.widget().height())
class Test(QWidget): class Test(QWidget):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
layout = QVBoxLayout() layout = QVBoxLayout()
self.resize(500, 600) self.resize(500, 600)
w1 = MyWidget() w1 = ChatWidget()
bm1 = BubbleMessage(
'你好啊噜啦噜啦嘞绿噜啦噜啦嘞绿绿噜啦噜啦嘞绿绿绿噜啦噜啦嘞绿绿绿绿',
"D:\Project\Python\PyQt-master\QLabel\Data\\fg1.png",
Type=1,
)
w1.add_message_item(bm1)
w2 = QLabel("nihao") w2 = QLabel("nihao")
layout.addWidget(w1) layout.addWidget(w1)
layout.addWidget(w2) layout.addWidget(w2)

View File

@ -1,8 +1,8 @@
from PyQt5.QtCore import QThread, pyqtSignal, Qt from PyQt5.QtCore import QThread, pyqtSignal, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QSpacerItem, QSizePolicy, QLabel, QHBoxLayout from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
from app.DataBase import msg from app.DataBase import msg
from app.components.bubble_message import BubbleMessage, ScrollBar, ScrollArea, ScrollAreaContent from app.components.bubble_message import BubbleMessage, ScrollBar, ScrollArea, ScrollAreaContent, ChatWidget
from app.person import MePC from app.person import MePC
@ -22,7 +22,11 @@ class ChatInfo(QWidget):
self.vBoxLayout = QVBoxLayout() self.vBoxLayout = QVBoxLayout()
self.vBoxLayout.setSpacing(0) self.vBoxLayout.setSpacing(0)
# self.vBoxLayout.addLayout(self.hBoxLayout) self.vBoxLayout.addLayout(self.hBoxLayout)
self.chat_window = ChatWidget()
self.vBoxLayout.addWidget(self.chat_window)
return
self.scrollArea = ScrollArea() self.scrollArea = ScrollArea()
self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
@ -30,7 +34,7 @@ class ChatInfo(QWidget):
self.scrollArea.setVerticalScrollBar(scrollBar) self.scrollArea.setVerticalScrollBar(scrollBar)
self.scrollAreaWidgetContents = ScrollAreaContent() self.scrollAreaWidgetContents = ScrollAreaContent()
self.scrollAreaWidgetContents.setMinimumSize(200, 10000) self.scrollAreaWidgetContents.setMinimumSize(200, 400)
self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.vBoxLayout.addWidget(self.scrollArea) self.vBoxLayout.addWidget(self.scrollArea)
@ -45,9 +49,10 @@ class ChatInfo(QWidget):
self.show_chat_thread.start() self.show_chat_thread.start()
def show_finish(self, ok): def show_finish(self, ok):
self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding) # self.spacerItem = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.scroolAreaLayout.addItem(self.spacerItem) # self.scroolAreaLayout.addItem(self.spacerItem)
self.setLayout(self.vBoxLayout) self.setLayout(self.vBoxLayout)
self.chat_window.set_scroll_bar_last()
def show_chat(self, message): def show_chat(self, message):
try: try:
@ -64,7 +69,8 @@ class ChatInfo(QWidget):
is_send is_send
) )
# print(str_content) # print(str_content)
self.scroolAreaLayout.addWidget(bubble_message) # self.scroolAreaLayout.addWidget(bubble_message)
self.chat_window.add_message_item(bubble_message)
except: except:
print(message) print(message)

View File

@ -58,7 +58,6 @@ class ChatWindow(QWidget, Ui_Form):
self.show_chats() self.show_chats()
def init_ui(self): def init_ui(self):
search_action = QAction(self.lineEdit) search_action = QAction(self.lineEdit)
search_action.setIcon(Icon.Search_Icon) search_action.setIcon(Icon.Search_Icon)
self.lineEdit.addAction(search_action, QLineEdit.LeadingPosition) self.lineEdit.addAction(search_action, QLineEdit.LeadingPosition)
@ -82,7 +81,7 @@ class ChatWindow(QWidget, Ui_Form):
self.ok_flag = True self.ok_flag = True
def show_chat(self, contact): def show_chat(self, contact):
contact_item = ContactQListWidgetItem(contact.nickName, contact.smallHeadImgUrl, contact.smallHeadImgBLOG) contact_item = ContactQListWidgetItem(contact.remark, contact.smallHeadImgUrl, contact.smallHeadImgBLOG)
self.listWidget.addItem(contact_item) self.listWidget.addItem(contact_item)
self.listWidget.setItemWidget(contact_item, contact_item.widget) self.listWidget.setItemWidget(contact_item, contact_item.widget)
chat_info_window = ChatInfo(contact) chat_info_window = ChatInfo(contact)

View File

@ -80,7 +80,7 @@ class ContactWindow(QWidget, Ui_Form):
self.ok_flag = True self.ok_flag = True
def show_contact(self, contact): def show_contact(self, contact):
contact_item = ContactQListWidgetItem(contact.nickName, contact.smallHeadImgUrl, contact.smallHeadImgBLOG) contact_item = ContactQListWidgetItem(contact.remark, contact.smallHeadImgUrl, contact.smallHeadImgBLOG)
self.listWidget.addItem(contact_item) self.listWidget.addItem(contact_item)
self.listWidget.setItemWidget(contact_item, contact_item.widget) self.listWidget.setItemWidget(contact_item, contact_item.widget)
contact_info_window = ContactInfo(contact) contact_info_window = ContactInfo(contact)