mirror of
https://gitee.com/Zhaoxin59/my-chat_-client.git
synced 2026-02-13 16:41:48 +08:00
add addFriendDialog and promote others
This commit is contained in:
@ -18,28 +18,39 @@ else()
|
||||
set(QT_VERSION_MAJOR 6)
|
||||
endif()
|
||||
|
||||
set(PROJECT_SOURCES
|
||||
main.cpp
|
||||
mainwidget.cpp
|
||||
mainwidget.h
|
||||
mainwidget.ui
|
||||
model/data.h
|
||||
resource.qrc
|
||||
sessionfriendarea.h
|
||||
sessionfriendarea.cpp
|
||||
debug.h
|
||||
messageshowarea.h
|
||||
messageshowarea.cpp
|
||||
messageeditarea.h
|
||||
messageeditarea.cpp
|
||||
SelfInfoWidget.h
|
||||
SelfInfoWidget.cpp
|
||||
userinfowidget.h
|
||||
userinfowidget.cpp
|
||||
sessiondetailwidget.h
|
||||
sessiondetailwidget.cpp
|
||||
choosefrienddialog.h
|
||||
choosefrienddialog.cpp
|
||||
#set(PROJECT_SOURCES
|
||||
# main.cpp
|
||||
# mainwidget.cpp
|
||||
# mainwidget.h
|
||||
# mainwidget.ui
|
||||
# model/data.h
|
||||
# resource.qrc
|
||||
# sessionfriendarea.h
|
||||
# sessionfriendarea.cpp
|
||||
# debug.h
|
||||
# messageshowarea.h
|
||||
# messageshowarea.cpp
|
||||
# messageeditarea.h
|
||||
# messageeditarea.cpp
|
||||
# SelfInfoWidget.h
|
||||
# SelfInfoWidget.cpp
|
||||
# userinfowidget.h
|
||||
# userinfowidget.cpp
|
||||
# sessiondetailwidget.h
|
||||
# sessiondetailwidget.cpp
|
||||
# choosefrienddialog.h
|
||||
# choosefrienddialog.cpp
|
||||
# groupsessiondetailwidget.h
|
||||
# groupsessiondetailwidget.cpp
|
||||
#)
|
||||
|
||||
|
||||
file(GLOB PROJECT_SOURCES
|
||||
model/*.h
|
||||
*.cpp
|
||||
*.h
|
||||
*.ui
|
||||
*.qrc
|
||||
)
|
||||
|
||||
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||
|
||||
172
addfrienddialog.cpp
Normal file
172
addfrienddialog.cpp
Normal file
@ -0,0 +1,172 @@
|
||||
#include "addfrienddialog.h"
|
||||
|
||||
|
||||
/////////////////////////////////////////
|
||||
//表示一个好友搜索的结果
|
||||
/////////////////////////////////////////
|
||||
FriendResultItem::FriendResultItem(const UserInfo& userInfo)
|
||||
:userInfo(userInfo)
|
||||
{
|
||||
// 1. 设置基本属性
|
||||
this->setFixedHeight(70);
|
||||
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
|
||||
// 2. 创建布局管理器
|
||||
QGridLayout* layout = new QGridLayout();
|
||||
layout->setVerticalSpacing(0);
|
||||
layout->setHorizontalSpacing(10);
|
||||
layout->setContentsMargins(0, 0, 20, 0);
|
||||
this->setLayout(layout);
|
||||
|
||||
// 3. 创建头像
|
||||
QPushButton* avatarBtn = new QPushButton();
|
||||
avatarBtn->setFixedSize(50, 50);
|
||||
avatarBtn->setIconSize(QSize(50, 50));
|
||||
avatarBtn->setIcon(userInfo.avatar);
|
||||
|
||||
// 4. 创建昵称
|
||||
QLabel* nameLabel = new QLabel();
|
||||
nameLabel->setFixedHeight(35); // 整个 Item 高度是 70. 昵称和个性签名各自占一半.
|
||||
nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700;}");
|
||||
nameLabel->setText(userInfo.nickname);
|
||||
|
||||
// 5. 创建个性签名
|
||||
QLabel* descLabel = new QLabel();
|
||||
descLabel->setFixedHeight(35);
|
||||
descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
descLabel->setStyleSheet("QLabel { font-size: 14px; }");
|
||||
descLabel->setText(userInfo.description);
|
||||
|
||||
// 6. 创建添加好友按钮
|
||||
addBtn = new QPushButton();
|
||||
addBtn->setFixedSize(100, 40);
|
||||
addBtn->setText("添加好友");
|
||||
QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px;} ";
|
||||
btnStyle += "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
|
||||
addBtn->setStyleSheet(btnStyle);
|
||||
|
||||
// 7. 把上述内容, 添加到布局管理器中
|
||||
layout->addWidget(avatarBtn, 0, 0, 2, 1);
|
||||
layout->addWidget(nameLabel, 0, 1);
|
||||
layout->addWidget(descLabel, 1, 1);
|
||||
layout->addWidget(addBtn, 0, 2, 2, 1);
|
||||
|
||||
// 8. 连接信号槽
|
||||
//connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clickAddBtn);
|
||||
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
//整个搜索好友的窗口
|
||||
/////////////////////////////////////////
|
||||
AddFriendDialog::AddFriendDialog(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
// 1. 设置基本属性
|
||||
this->setFixedSize(500, 500);
|
||||
this->setWindowTitle("添加好友");
|
||||
this->setWindowIcon(QIcon(":/resource/image/logo.png"));
|
||||
this->setStyleSheet("QDialog {background-color: rgb(255, 255, 255); }");
|
||||
this->setAttribute(Qt::WA_DeleteOnClose); // 不要忘记这个属性!!!
|
||||
|
||||
// 2. 添加布局管理器
|
||||
layout = new QGridLayout();
|
||||
layout->setSpacing(10);
|
||||
layout->setContentsMargins(20, 20, 20, 0);
|
||||
this->setLayout(layout);
|
||||
|
||||
// 3. 创建搜索框
|
||||
searchEdit = new QLineEdit();
|
||||
searchEdit->setFixedHeight(50);
|
||||
searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
QString style = "QLineEdit { border: none; border-radius: 10px; font-size: 16px; color: rgb(129, 129, 129); background-color: rgb(240, 240, 240); padding-left: 5px;}";
|
||||
searchEdit->setStyleSheet(style);
|
||||
searchEdit->setPlaceholderText("按手机号/用户序号/昵称搜索");
|
||||
layout->addWidget(searchEdit, 0, 0, 1, 8);
|
||||
|
||||
// 4. 创建搜索按钮
|
||||
QPushButton* searchBtn = new QPushButton();
|
||||
searchBtn->setFixedSize(50, 50);
|
||||
searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
searchBtn->setIconSize(QSize(30, 30));
|
||||
searchBtn->setIcon(QIcon(":/resource/image/search.png"));
|
||||
QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
|
||||
btnStyle += "QPushButton:hover { background-color: rgb(220, 220, 220); } QPushButton:pressed { background-color: rgb(200, 200, 200); } ";
|
||||
searchBtn->setStyleSheet(btnStyle);
|
||||
layout->addWidget(searchBtn, 0, 8, 1, 1);
|
||||
|
||||
// 5. 添加滚动区域
|
||||
initResultArea();
|
||||
|
||||
// 构造假的数据, 验证界面效果
|
||||
#if TEST_UI
|
||||
QIcon avatar(":/resource/image/defaultAvatar.png");
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
// new 出来这个对象, 再往 addResult 中添加. FriendResultItem 中持有了 UserInfo 的 const 引用. 需要确保引用是有效的引用
|
||||
UserInfo* userInfo = new UserInfo();
|
||||
userInfo->userId = QString::number(1000 + i);
|
||||
userInfo->nickname = "张三" + QString::number(i);
|
||||
userInfo->description = "这是一段个性签名";
|
||||
userInfo->avatar = avatar;
|
||||
this->addResult(*userInfo);
|
||||
}
|
||||
#endif
|
||||
//
|
||||
// // 6. 连接信号槽
|
||||
// connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::clickSearchBtn);
|
||||
|
||||
}
|
||||
|
||||
void AddFriendDialog::initResultArea()
|
||||
{
|
||||
// 1. 创建滚动区域对象
|
||||
QScrollArea* scrollArea = new QScrollArea();
|
||||
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
scrollArea->setWidgetResizable(true);
|
||||
scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0;} ");
|
||||
scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width: 2px; background-color: rgb(255, 255, 255);}");
|
||||
scrollArea->setStyleSheet("QScrollArea { border: none; }");
|
||||
layout->addWidget(scrollArea, 1, 0, 1, 9);
|
||||
|
||||
// 2. 创建 QWidget
|
||||
resultContainer = new QWidget();
|
||||
resultContainer->setObjectName("resultContainer");
|
||||
resultContainer->setStyleSheet("#resultContainer { background-color: rgb(255, 255, 255); } ");
|
||||
scrollArea->setWidget(resultContainer);
|
||||
|
||||
// 3. 给这个 QWidget 里面添加元素, 需要给它创建垂直的布局管理器
|
||||
QVBoxLayout* vlayout = new QVBoxLayout();
|
||||
vlayout->setSpacing(0);
|
||||
vlayout->setContentsMargins(0, 0, 0, 0);
|
||||
resultContainer->setLayout(vlayout);
|
||||
|
||||
}
|
||||
|
||||
void AddFriendDialog::addResult(const UserInfo& userInfo)
|
||||
{
|
||||
FriendResultItem* item = new FriendResultItem(userInfo);
|
||||
resultContainer->layout()->addWidget(item);
|
||||
}
|
||||
|
||||
void AddFriendDialog::clear()
|
||||
{
|
||||
// 从后向前遍历
|
||||
QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(resultContainer->layout());
|
||||
for (int i = layout->count() - 1; i >= 0; i--) {
|
||||
QLayoutItem* layoutItem = layout->takeAt(i);
|
||||
if (layoutItem == nullptr || layoutItem->widget() == nullptr) {
|
||||
continue;
|
||||
}
|
||||
//删除这里面所持有的元素
|
||||
delete layoutItem->widget();
|
||||
}
|
||||
}
|
||||
|
||||
void AddFriendDialog::setSearchKey(const QString& searcheKey)
|
||||
{
|
||||
searchEdit->setText(searcheKey);
|
||||
}
|
||||
|
||||
62
addfrienddialog.h
Normal file
62
addfrienddialog.h
Normal file
@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
#include <QWidget>
|
||||
#include <QGridLayout>
|
||||
#include <QPushButton>
|
||||
#include <QLineEdit>
|
||||
#include <QLabel>
|
||||
#include <QScrollArea>
|
||||
#include <QScrollBar>
|
||||
|
||||
#include "debug.h"
|
||||
#include "model/data.h"
|
||||
|
||||
using model::UserInfo;
|
||||
|
||||
/////////////////////////////////////////
|
||||
//表示一个好友搜索的结果
|
||||
/////////////////////////////////////////
|
||||
class FriendResultItem : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
FriendResultItem(const UserInfo& userInfo);
|
||||
|
||||
private:
|
||||
const UserInfo& userInfo;
|
||||
|
||||
QPushButton* addBtn;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////
|
||||
//整个搜索好友的窗口
|
||||
/////////////////////////////////////////
|
||||
class AddFriendDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
AddFriendDialog(QWidget *parent);
|
||||
|
||||
//初始化结果区域
|
||||
void initResultArea();
|
||||
|
||||
//往窗口中新增一个好友搜索的结果
|
||||
void addResult(const UserInfo& userInfo);
|
||||
|
||||
//清空界面上所有的好友搜索结果
|
||||
void clear();
|
||||
|
||||
//
|
||||
void setSearchKey(const QString& searcheKey);
|
||||
|
||||
private:
|
||||
//整个窗口的网格布局
|
||||
QGridLayout* layout;
|
||||
|
||||
//
|
||||
QLineEdit* searchEdit;
|
||||
|
||||
//保存搜索好友的结果
|
||||
QWidget* resultContainer;
|
||||
};
|
||||
@ -4,7 +4,8 @@
|
||||
////////////////////////////////////////////////
|
||||
/// 选择好友窗口中的一个 元素/好友项
|
||||
////////////////////////////////////////////////
|
||||
ChooseFriendItem::ChooseFriendItem(ChooseFriendDialog* owner, const QIcon& avatar, const QString& name, bool checked)
|
||||
ChooseFriendItem::ChooseFriendItem(ChooseFriendDialog* owner, const QString& userId, const QIcon& avatar, const QString& name, bool checked)
|
||||
:userId(userId)
|
||||
{
|
||||
// 1. 设置控件的基本属性
|
||||
this->setFixedHeight(50);
|
||||
@ -44,11 +45,11 @@ ChooseFriendItem::ChooseFriendItem(ChooseFriendDialog* owner, const QIcon& avata
|
||||
connect(checkBox, &QCheckBox::toggled, this, [=](bool checked) {
|
||||
if (checked) {
|
||||
// 勾选了复选框, 把当前这个 Item, 添加到右侧的已选择区域
|
||||
owner->addSelectedFriend(avatar, name);
|
||||
owner->addSelectedFriend(userId, avatar, name);
|
||||
}
|
||||
else {
|
||||
// 取消勾选
|
||||
//owner->deleteSelectedFriend(userId);
|
||||
owner->deleteSelectedFriend(userId);
|
||||
}
|
||||
});
|
||||
|
||||
@ -74,7 +75,7 @@ void ChooseFriendItem::enterEvent(QEnterEvent* event)
|
||||
isHover = true;
|
||||
//相当于界面更新
|
||||
this->update();
|
||||
//this->repaint();
|
||||
//this->repaint(); //也可以
|
||||
}
|
||||
|
||||
void ChooseFriendItem::leaveEvent(QEvent* event)
|
||||
@ -138,7 +139,7 @@ void ChooseFriendDialog::initLeft(QHBoxLayout* layout)
|
||||
#if TEST_UI
|
||||
QIcon defaultAvatar(":/resource/image/defaultAvatar.png");
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
this->addFriend(defaultAvatar, "张三" + QString::number(i), false);
|
||||
this->addFriend(QString::number(1000 + i), defaultAvatar, "张三" + QString::number(i), false);
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -208,7 +209,7 @@ void ChooseFriendDialog::initRight(QHBoxLayout* layout)
|
||||
// 此处的数据通过勾选左侧列表来生成.
|
||||
QIcon defaultAvatar(":/resource/image/defaultAvatar.png");
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
this->addSelectedFriend(defaultAvatar, "张三" + QString::number(i));
|
||||
this->addSelectedFriend(QString::number(1000 + i), defaultAvatar, "张三" + QString::number(i));
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -220,14 +221,54 @@ void ChooseFriendDialog::initRight(QHBoxLayout* layout)
|
||||
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::addFriend(const QIcon& avatar, const QString& name, bool checked)
|
||||
void ChooseFriendDialog::addFriend(const QString& userId, const QIcon& avatar, const QString& name, bool checked)
|
||||
{
|
||||
ChooseFriendItem* item = new ChooseFriendItem(this, avatar, name, checked);
|
||||
ChooseFriendItem* item = new ChooseFriendItem(this, userId, avatar, name, checked);
|
||||
totalContainer->layout()->addWidget(item);
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::addSelectedFriend(const QIcon& avatar, const QString& name)
|
||||
void ChooseFriendDialog::addSelectedFriend(const QString& userId, const QIcon& avatar, const QString& name)
|
||||
{
|
||||
ChooseFriendItem* item = new ChooseFriendItem(this, avatar, name, true);
|
||||
ChooseFriendItem* item = new ChooseFriendItem(this, userId, avatar, name, true);
|
||||
selectedContainer->layout()->addWidget(item);
|
||||
}
|
||||
|
||||
void ChooseFriendDialog::deleteSelectedFriend(const QString& userId)
|
||||
{
|
||||
//遍历selectedContainer中的所有的Item,并对比其userId
|
||||
QVBoxLayout* vlayout = dynamic_cast<QVBoxLayout*>(selectedContainer->layout());
|
||||
//由于是要遍历加删除所以,要从后向前进行
|
||||
for (int i = vlayout->count() - 1; i >= 0; --i) {
|
||||
auto* item = vlayout->itemAt(i);
|
||||
if (item == nullptr || item->widget() == nullptr) {
|
||||
continue;
|
||||
}
|
||||
ChooseFriendItem* chooseFriendItem = dynamic_cast<ChooseFriendItem*>(item->widget());
|
||||
//判定当前的Item的userId是否是要删除的userId
|
||||
if (chooseFriendItem->getUserId() != userId) {
|
||||
continue;
|
||||
}
|
||||
vlayout->removeWidget(chooseFriendItem);
|
||||
//会报错!!!
|
||||
// 要释放对象,不是直接delete,而是告诉qt让qt在信号槽这一轮
|
||||
// 执行完成后,自行负责释放
|
||||
//delete chooseFriendItem;
|
||||
chooseFriendItem->deleteLater();
|
||||
}
|
||||
|
||||
//再遍历一下左侧的列表,把左侧对应的item的checkBox的勾选状态给取消
|
||||
QVBoxLayout* vlayoutLeft = dynamic_cast<QVBoxLayout*>(totalContainer->layout());
|
||||
for(int i = 0; i < vlayoutLeft->count(); i++) {
|
||||
auto* item = vlayoutLeft->itemAt(i);
|
||||
if (item == nullptr || item->widget() == nullptr) {
|
||||
continue;
|
||||
}
|
||||
ChooseFriendItem* chooseFriendItem = dynamic_cast<ChooseFriendItem*>(item->widget());
|
||||
if (chooseFriendItem->getUserId() != userId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//已找到,取消勾选状态
|
||||
chooseFriendItem->getCheckBox()->setChecked(false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,12 +20,20 @@ class ChooseFriendDialog;
|
||||
class ChooseFriendItem : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
ChooseFriendItem(ChooseFriendDialog* owner, const QIcon& avatar, const QString& name, bool checked);
|
||||
ChooseFriendItem(ChooseFriendDialog* owner, const QString& userId, const QIcon& avatar, const QString& name, bool checked);
|
||||
|
||||
void paintEvent(QPaintEvent* event) override;
|
||||
void enterEvent(QEnterEvent* event) override;
|
||||
void leaveEvent(QEvent* event) override;
|
||||
|
||||
QString& getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
QCheckBox* getCheckBox() {
|
||||
return checkBox;
|
||||
}
|
||||
|
||||
private:
|
||||
bool isHover = false;
|
||||
|
||||
@ -33,6 +41,8 @@ private:
|
||||
QPushButton* avatarBtn;
|
||||
QLabel* nameLabel;
|
||||
ChooseFriendDialog* owner; //记录了哪个QWidget持有这个Item,此处应该是ChooseFriendDialog
|
||||
|
||||
QString userId; //记录成员id用于删除唯一指定的成员Item
|
||||
};
|
||||
|
||||
class ChooseFriendDialog : public QDialog
|
||||
@ -45,8 +55,9 @@ public:
|
||||
void initLeft(QHBoxLayout* layout);
|
||||
void initRight(QHBoxLayout* layout);
|
||||
|
||||
void addFriend(const QIcon& avatar, const QString& name, bool checked);
|
||||
void addSelectedFriend(const QIcon& avatar, const QString& name);
|
||||
void addFriend(const QString& userId, const QIcon& avatar, const QString& name, bool checked);
|
||||
void addSelectedFriend(const QString& userId, const QIcon& avatar, const QString& name);
|
||||
void deleteSelectedFriend(const QString& userId);
|
||||
|
||||
private:
|
||||
QWidget* totalContainer;
|
||||
|
||||
2
debug.h
2
debug.h
@ -3,4 +3,6 @@
|
||||
|
||||
#define TEST_UI 1
|
||||
|
||||
#define TEST_GROUP_SESSION_DETAIL 1
|
||||
|
||||
#endif // DEBUG_H
|
||||
|
||||
115
groupsessiondetailwidget.cpp
Normal file
115
groupsessiondetailwidget.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "groupsessiondetailwidget.h"
|
||||
|
||||
GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
//设置窗口的基本属性
|
||||
this->setFixedSize(410, 600);
|
||||
this->setWindowTitle("群组详情");
|
||||
this->setWindowIcon(QIcon(":resource/image/logo.png"));
|
||||
this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
|
||||
this->setAttribute(Qt::WA_DeleteOnClose);
|
||||
|
||||
//创建界面布局管理器
|
||||
QVBoxLayout* vlayout = new QVBoxLayout();
|
||||
vlayout->setSpacing(10);
|
||||
vlayout->setContentsMargins(50, 20, 50, 50);
|
||||
vlayout->setAlignment(Qt::AlignTop);
|
||||
this->setLayout(vlayout);
|
||||
|
||||
//创建滚动区域
|
||||
//创建qscrollarea的对象
|
||||
QScrollArea* scrollArea = new QScrollArea();
|
||||
scrollArea->setWidgetResizable(true);
|
||||
scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");
|
||||
scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; }");
|
||||
scrollArea->setFixedSize(310, 350);
|
||||
scrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
||||
scrollArea->setStyleSheet("QWidget { background-color: transparent; border: none; }");
|
||||
|
||||
//创建一个qscrollarea的内部的qwidget
|
||||
QWidget* container = new QWidget();
|
||||
scrollArea->setWidget(container);
|
||||
|
||||
//给container添加一个网格布局
|
||||
glayout = new QGridLayout();
|
||||
glayout->setSpacing(0);
|
||||
glayout->setContentsMargins(0, 0, 0, 0);
|
||||
glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
||||
container->setLayout(glayout);
|
||||
|
||||
//把滚动区域, 添加到布局管理器中
|
||||
vlayout->addWidget(scrollArea);
|
||||
|
||||
//添加按钮
|
||||
AvatarItem* addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
|
||||
glayout->addWidget(addBtn, 0, 0);
|
||||
|
||||
//添加群聊名称
|
||||
QLabel* groupNameTag = new QLabel();
|
||||
groupNameTag->setText("群聊名称");
|
||||
groupNameTag->setStyleSheet("QLabel {font-weight: 700; font-size: 16px;}");
|
||||
groupNameTag->setFixedHeight(50);
|
||||
groupNameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
||||
// 设置文字在 QLabel 内部的对齐方式.
|
||||
groupNameTag->setAlignment(Qt::AlignBottom);
|
||||
// 这里设置的 QLabel 在布局管理器中的对齐方式.
|
||||
vlayout->addWidget(groupNameTag);
|
||||
|
||||
//添加群聊的真实名字和修改按钮
|
||||
//创建水平布局
|
||||
QHBoxLayout* hlayout = new QHBoxLayout();
|
||||
hlayout->setSpacing(0);
|
||||
hlayout->setContentsMargins(0, 0, 0, 0);
|
||||
vlayout->addLayout(hlayout);
|
||||
|
||||
//创建群聊名字的label
|
||||
QLabel* groupNameLabel = new QLabel();
|
||||
groupNameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
|
||||
groupNameLabel->setFixedHeight(50);
|
||||
groupNameLabel->setStyleSheet("QLabel { font-size: 18px; }");
|
||||
hlayout->addWidget(groupNameLabel);
|
||||
|
||||
//创建修改按钮
|
||||
QPushButton* modifyBtn = new QPushButton();
|
||||
modifyBtn->setFixedSize(30, 30);
|
||||
modifyBtn->setIconSize(QSize(20, 20));
|
||||
modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
|
||||
modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
|
||||
modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(230, 230, 230); }");
|
||||
hlayout->addWidget(modifyBtn, 0, Qt::AlignRight | Qt::AlignVCenter);
|
||||
|
||||
// 退出群聊按钮
|
||||
QPushButton* exitGroupBtn = new QPushButton();
|
||||
exitGroupBtn->setText("退出群聊");
|
||||
exitGroupBtn->setFixedHeight(50);
|
||||
exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||
QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; background-color: transparent; color: rgb(0, 0, 0); }";
|
||||
btnStyle += "QPushButton:pressed { background-color: rgb(230, 230, 230); }";
|
||||
exitGroupBtn->setStyleSheet(btnStyle);
|
||||
vlayout->addWidget(exitGroupBtn);
|
||||
|
||||
|
||||
#if TEST_UI
|
||||
groupNameLabel->setText("人类吃喝行为研究小组");
|
||||
QIcon avatar(":/resource/image/defaultAvatar.png");
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
AvatarItem* item = new AvatarItem(avatar, "张三" + QString::number(i));
|
||||
this->addMember(item);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void GroupSessionDetailWidget::addMember(AvatarItem* avatarItem)
|
||||
{
|
||||
//拿到滚动区域的布局管理器
|
||||
const int MAX_COL = 4;
|
||||
if (curCol >= MAX_COL) {
|
||||
// 换行操作
|
||||
++curRow;
|
||||
curCol = 0;
|
||||
}
|
||||
glayout->addWidget(avatarItem, curRow, curCol);
|
||||
++curCol;
|
||||
|
||||
}
|
||||
32
groupsessiondetailwidget.h
Normal file
32
groupsessiondetailwidget.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
#include <QWidget>
|
||||
|
||||
#include <QVBoxLayout>
|
||||
#include <qscrollarea.h>
|
||||
#include <qscrollbar.h>
|
||||
#include <qgridlayout.h>
|
||||
#include <qpushbutton.h>
|
||||
#include <qgridlayout.h>
|
||||
#include "sessiondetailwidget.h"
|
||||
#include "debug.h"
|
||||
|
||||
class AvatarItem;
|
||||
|
||||
class GroupSessionDetailWidget : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GroupSessionDetailWidget(QWidget* parent);
|
||||
|
||||
void addMember(AvatarItem* avatarItem);
|
||||
|
||||
private:
|
||||
QGridLayout* glayout;
|
||||
|
||||
//表示当前所要添加的AvatarItem处在的行和列
|
||||
int curRow = 0;
|
||||
int curCol = 1;
|
||||
};
|
||||
2
main.cpp
2
main.cpp
@ -8,6 +8,8 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
qputenv("QT_QPA_PLATFORM", "windows:darkmode=0");
|
||||
|
||||
LOG() << "Hello";
|
||||
|
||||
QPalette palette;
|
||||
|
||||
@ -129,7 +129,7 @@ void MainWidget::initMidWindow()
|
||||
searchEdit = new QLineEdit();
|
||||
searchEdit->setFixedHeight(30);
|
||||
searchEdit->setPlaceholderText("搜索");
|
||||
searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; background-color: rgb(226, 226, 226); padding-left: 5px; }");
|
||||
searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; color: rgb(129, 129, 129); background-color: rgb(226, 226, 226); padding-left: 5px; }");
|
||||
|
||||
addFriendBtn = new QPushButton();
|
||||
addFriendBtn->setFixedSize(30, 30);
|
||||
@ -242,8 +242,41 @@ void MainWidget::initSignalSlot()
|
||||
// 点击右上角的更多按钮,扩展会话的详细信息
|
||||
/////////////////////////////////////
|
||||
connect(extraBtn, &QPushButton::clicked, this, [=]() {
|
||||
//判定当前的会话是单聊还是群聊
|
||||
#if TEST_GROUP_SESSION_DETAIL
|
||||
bool isSingleChat = false; //要根据当前选中的实际的会话来确定
|
||||
#else
|
||||
bool isSingleChat = true; //要根据当前选中的实际的会话来确定
|
||||
#endif
|
||||
if (isSingleChat) {
|
||||
//说明是单聊
|
||||
SessionDetailWidget* sessionDetailWidget = new SessionDetailWidget(this);
|
||||
sessionDetailWidget->exec();
|
||||
}
|
||||
else {
|
||||
//说明是群聊
|
||||
GroupSessionDetailWidget* groupSessionDetailWidget = new GroupSessionDetailWidget(this);
|
||||
groupSessionDetailWidget->exec();
|
||||
}
|
||||
});
|
||||
|
||||
/////////////////////////////////////
|
||||
// 点击添加好友按钮,弹出添加好友的窗口
|
||||
/////////////////////////////////////
|
||||
connect(addFriendBtn, &QPushButton::clicked, this, [=]() {
|
||||
AddFriendDialog* addFriendDialog = new AddFriendDialog(this);
|
||||
addFriendDialog->exec();
|
||||
});
|
||||
|
||||
/////////////////////////////////////
|
||||
// 修改搜索框内容,设置到新弹出的输入框里面
|
||||
/////////////////////////////////////
|
||||
connect(searchEdit, &QLineEdit::textEdited, this, [=]() {
|
||||
const QString& searchKey = searchEdit->text();
|
||||
AddFriendDialog* addFriendDialog = new AddFriendDialog(this);
|
||||
addFriendDialog->setSearchKey(searchKey);
|
||||
searchEdit->setText("");
|
||||
addFriendDialog->exec();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -11,6 +11,8 @@
|
||||
#include "messageshowarea.h"
|
||||
#include "SelfInfoWidget.h"
|
||||
#include "sessionfriendarea.h"
|
||||
#include "groupsessiondetailwidget.h"
|
||||
#include "addfrienddialog.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
|
||||
Reference in New Issue
Block a user