mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-14 00:51:48 +08:00
promote ui
This commit is contained in:
71
messageshowarea.cpp
Normal file
71
messageshowarea.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include "messageshowarea.h"
|
||||
|
||||
MessageShowArea::MessageShowArea() {
|
||||
//初始化基本属性
|
||||
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
this->setWidgetResizable(true);
|
||||
|
||||
//设置滚动条样式
|
||||
this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(240, 240, 240); }");
|
||||
this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; }");
|
||||
this->setStyleSheet("QScrollArea { border: none; }");
|
||||
|
||||
//创建container这样的widget,作为包含内部元素的容器
|
||||
container = new QWidget();
|
||||
this->setWidget(container);
|
||||
|
||||
//给container添加界面布局管理器
|
||||
QVBoxLayout* layout = new QVBoxLayout();
|
||||
layout->setSpacing(0);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
container->setLayout(layout);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////
|
||||
/// 表示一个消息元素
|
||||
////////////////////////////////////////////
|
||||
MessageItem::MessageItem(bool isLeft)
|
||||
:isLeft(isLeft)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MessageItem *MessageItem::makeMessageItem(bool isLeft, const Message &message)
|
||||
{
|
||||
//创建对象和布局管理器
|
||||
MessageItem* messageItem = new MessageItem(isLeft);
|
||||
QGridLayout* layout = new QGridLayout();
|
||||
layout->setSpacing(0);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
//这个message最低不能低于100
|
||||
messageItem->setMinimumHeight(100);
|
||||
messageItem->setLayout(layout);
|
||||
|
||||
//创建头像
|
||||
QPushButton* avatarBtn = new QPushButton();
|
||||
avatarBtn->setFixedSize(40, 40);
|
||||
avatarBtn->setIconSize(QSize(40, 40));
|
||||
avatarBtn->setIcon(message.sender.avatar);
|
||||
if(isLeft) {
|
||||
layout->addWidget(avatarBtn, 0, 0, 2, 1, Qt::AlignTop | Qt::AlignLeft);
|
||||
} else {
|
||||
layout->addWidget(avatarBtn, 0, 1, 2, 1, Qt::AlignTop | Qt::AlignRight);
|
||||
}
|
||||
|
||||
//创建名字和时间
|
||||
QLabel* nameLabel = new QLabel();
|
||||
nameLabel->setText(message.sender.nickname + " | " + message.time);
|
||||
nameLabel->setAlignment(Qt::AlignBottom);
|
||||
nameLabel->setStyleSheet("QLabel { font-size: 12px; color: rgb(178, 178, 178); }");
|
||||
if(isLeft) {
|
||||
layout->addWidget(nameLabel, 0, 1);
|
||||
} else {
|
||||
layout->addWidget(nameLabel, 0, 0, Qt::AlignRight);
|
||||
}
|
||||
|
||||
//创建消息体
|
||||
|
||||
|
||||
return messageItem;
|
||||
}
|
||||
Reference in New Issue
Block a user