├── Client
├── ...
├── 111.qrc
├── FileTransferContext.h
├── NewCharClient.pro
├── NewCharClient.pro.user
├── addfriend.cpp
├── addfriend.h
├── addfriend.ui
├── attnitem.cpp
├── attnitem.h
├── attnitem.ui
├── chat
│ └── ...
├── chatplaintext.cpp
├── chatplaintext.h
├── chatplaintext.ui
├── form.cpp
├── form.h
├── form.ui
├── frienditem.cpp
├── frienditem.h
├── frienditem.ui
├── friendsendfile.cpp
├── friendsendfile.h
├── friendsendfile.ui
├── home.cpp
├── home.h
├── home.ui
├── icon
│ ├── ...
│ ├── 1.png
│ ├── 10.png
│ ├── 11.png
│ ├── 1112.png
│ ├── 12.png
│ ├── 13.png
│ ├── 14.png
│ ├── 145.png
│ ├── 15.png
│ ├── 155.png
│ ├── 16.png
│ ├── 17.png
│ ├── 18.png
│ ├── 19.png
│ ├── 2.png
│ ├── 20.png
│ ├── 21.png
│ ├── 22.png
│ ├── 23.png
│ ├── 24.png
│ ├── 25.png
│ ├── 26.png
│ ├── 27.png
│ ├── 28.png
│ ├── 29.png
│ ├── 3.png
│ ├── 30.png
│ ├── 31.png
│ ├── 32.png
│ ├── 33.png
│ ├── 34.png
│ ├── 35.png
│ ├── 36.png
│ ├── 37.png
│ ├── 38.png
│ ├── 39.png
│ ├── 4.png
│ ├── 40.png
│ ├── 41.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ ├── 8.png
│ ├── 9.png
│ └── R-C.png
├── isfriend.cpp
├── isfriend.h
├── isfriend.ui
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
├── myinformation.cpp
├── myinformation.h
├── myinformation.ui
├── mysendfile.cpp
├── mysendfile.h
├── mysendfile.ui
├── prompt.cpp
├── prompt.h
├── prompt.ui
├── register.cpp
├── register.h
├── register.ui
├── senddata.cpp
├── senddata.h
├── stickerwidget.cpp
├── stickerwidget.h
└── stickerwidget.ui
├── MySQL
├── ...
├── friendships.ibd
└── users.ibd
├── README.md
└── Server
├── ...
├── FileTransferContext.h
├── NewCharServer.pro
├── NewCharServer.pro.user
├── linksql.cpp
├── linksql.h
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
├── mainwindow.ui
└── usericon
├── ...
├── 1.png
├── 10018.png
├── 10022.png
├── 10023.png
├── 10045.png
├── 10046.png
├── 10047.png
├── 10048.png
└── 10049.png
/Client/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Client/111.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | icon/1.png
4 | icon/2.png
5 | icon/3.png
6 | icon/4.png
7 | icon/5.png
8 | icon/6.png
9 | icon/7.png
10 | icon/8.png
11 | icon/9.png
12 | icon/10.png
13 | icon/11.png
14 | icon/12.png
15 | icon/13.png
16 | icon/14.png
17 | icon/15.png
18 | icon/16.png
19 | icon/17.png
20 | icon/18.png
21 | icon/19.png
22 | icon/20.png
23 | icon/21.png
24 | icon/22.png
25 | icon/23.png
26 | icon/24.png
27 | icon/25.png
28 | icon/26.png
29 | icon/27.png
30 | icon/28.png
31 | icon/29.png
32 | icon/30.png
33 | icon/31.png
34 | icon/32.png
35 | icon/33.png
36 | icon/34.png
37 | icon/35.png
38 | icon/36.png
39 | icon/37.png
40 | icon/38.png
41 | icon/39.png
42 | icon/40.png
43 | icon/41.png
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Client/FileTransferContext.h:
--------------------------------------------------------------------------------
1 | #ifndef FILETRANSFERCONTEXT_H
2 | #define FILETRANSFERCONTEXT_H
3 | #include
4 | class FileTransferContext{
5 | public:
6 | QString type;
7 | qint64 fileId;//文件唯一id
8 | qint64 totalSize;//文件大小
9 | qint64 receivedSize;//当前接收大小
10 | QByteArray data;//文件内容
11 | int friendid;
12 | QString friendname;
13 | QString path;
14 | int h;
15 | int w;
16 |
17 | bool operator==(const FileTransferContext &other)const{
18 | return this->fileId==other.fileId;
19 | }
20 | };
21 |
22 | class BeforFiledata{
23 | public:
24 | qint64 fileid;
25 | QByteArray data;
26 |
27 | bool operator==(const BeforFiledata &other)const{
28 | if(this->fileid==other.fileid&&this->data==other.data){
29 | return 1;
30 | }else{
31 | return 0;
32 | }
33 | }
34 | };
35 |
36 | #endif // FILETRANSFERCONTEXT_H
37 |
--------------------------------------------------------------------------------
/Client/NewCharClient.pro:
--------------------------------------------------------------------------------
1 | QT += core gui
2 | QT+=network
3 |
4 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
5 |
6 | CONFIG += c++17
7 |
8 | # You can make your code fail to compile if it uses deprecated APIs.
9 | # In order to do so, uncomment the following line.
10 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
11 |
12 | SOURCES += \
13 | addfriend.cpp \
14 | attnitem.cpp \
15 | chatplaintext.cpp \
16 | form.cpp \
17 | frienditem.cpp \
18 | friendsendfile.cpp \
19 | home.cpp \
20 | isfriend.cpp \
21 | main.cpp \
22 | mainwindow.cpp \
23 | myinformation.cpp \
24 | mysendfile.cpp \
25 | prompt.cpp \
26 | register.cpp \
27 | senddata.cpp \
28 | stickerwidget.cpp
29 |
30 | HEADERS += \
31 | FileTransferContext.h \
32 | addfriend.h \
33 | attnitem.h \
34 | chatplaintext.h \
35 | form.h \
36 | frienditem.h \
37 | friendsendfile.h \
38 | home.h \
39 | isfriend.h \
40 | mainwindow.h \
41 | myinformation.h \
42 | mysendfile.h \
43 | prompt.h \
44 | register.h \
45 | senddata.h \
46 | stickerwidget.h
47 |
48 | FORMS += \
49 | addfriend.ui \
50 | attnitem.ui \
51 | chatplaintext.ui \
52 | form.ui \
53 | frienditem.ui \
54 | friendsendfile.ui \
55 | home.ui \
56 | isfriend.ui \
57 | mainwindow.ui \
58 | myinformation.ui \
59 | mysendfile.ui \
60 | prompt.ui \
61 | register.ui \
62 | stickerwidget.ui
63 |
64 | # Default rules for deployment.
65 | qnx: target.path = /tmp/$${TARGET}/bin
66 | else: unix:!android: target.path = /opt/$${TARGET}/bin
67 | !isEmpty(target.path): INSTALLS += target
68 |
69 | RESOURCES += \
70 | 111.qrc
71 |
--------------------------------------------------------------------------------
/Client/NewCharClient.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {e0c42ccd-68bc-4fea-9627-6cbc6905b16b}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 0
37 | 80
38 | true
39 | true
40 | 1
41 | 0
42 | false
43 | true
44 | false
45 | 2
46 | true
47 | true
48 | 0
49 | 8
50 | true
51 | false
52 | 1
53 | true
54 | true
55 | true
56 | *.md, *.MD, Makefile
57 | false
58 | true
59 | true
60 |
61 |
62 |
63 | ProjectExplorer.Project.PluginSettings
64 |
65 |
66 | true
67 | false
68 | true
69 | true
70 | true
71 | true
72 |
73 | false
74 |
75 |
76 | 0
77 | true
78 |
79 | true
80 | true
81 | Builtin.DefaultTidyAndClazy
82 | 8
83 | true
84 |
85 |
86 |
87 | true
88 |
89 |
90 |
91 |
92 | ProjectExplorer.Project.Target.0
93 |
94 | Desktop
95 | Desktop Qt 6.5.3 MinGW 64-bit
96 | Desktop Qt 6.5.3 MinGW 64-bit
97 | qt.qt6.653.win64_mingw_kit
98 | 0
99 | 0
100 | 0
101 |
102 | 0
103 | F:\newChar\NewCharClient\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug
104 | F:/newChar/NewCharClient/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug
105 |
106 |
107 | true
108 | QtProjectManager.QMakeBuildStep
109 | false
110 |
111 |
112 |
113 | true
114 | Qt4ProjectManager.MakeStep
115 |
116 | 2
117 | 构建
118 | 构建
119 | ProjectExplorer.BuildSteps.Build
120 |
121 |
122 |
123 | true
124 | Qt4ProjectManager.MakeStep
125 | clean
126 |
127 | 1
128 | 清除
129 | 清除
130 | ProjectExplorer.BuildSteps.Clean
131 |
132 | 2
133 | false
134 |
135 | false
136 |
137 | Debug
138 | Qt4ProjectManager.Qt4BuildConfiguration
139 | 2
140 |
141 |
142 | F:\newChar\NewCharClient\build\Desktop_Qt_6_5_3_MinGW_64_bit-Release
143 | F:/newChar/NewCharClient/build/Desktop_Qt_6_5_3_MinGW_64_bit-Release
144 |
145 |
146 | true
147 | QtProjectManager.QMakeBuildStep
148 | false
149 |
150 |
151 |
152 | true
153 | Qt4ProjectManager.MakeStep
154 |
155 | 2
156 | 构建
157 | 构建
158 | ProjectExplorer.BuildSteps.Build
159 |
160 |
161 |
162 | true
163 | Qt4ProjectManager.MakeStep
164 | clean
165 |
166 | 1
167 | 清除
168 | 清除
169 | ProjectExplorer.BuildSteps.Clean
170 |
171 | 2
172 | false
173 |
174 | false
175 |
176 | Release
177 | Qt4ProjectManager.Qt4BuildConfiguration
178 | 0
179 | 0
180 |
181 |
182 | 0
183 | F:\newChar\NewCharClient\build\Desktop_Qt_6_5_3_MinGW_64_bit-Profile
184 | F:/newChar/NewCharClient/build/Desktop_Qt_6_5_3_MinGW_64_bit-Profile
185 |
186 |
187 | true
188 | QtProjectManager.QMakeBuildStep
189 | false
190 |
191 |
192 |
193 | true
194 | Qt4ProjectManager.MakeStep
195 |
196 | 2
197 | 构建
198 | 构建
199 | ProjectExplorer.BuildSteps.Build
200 |
201 |
202 |
203 | true
204 | Qt4ProjectManager.MakeStep
205 | clean
206 |
207 | 1
208 | 清除
209 | 清除
210 | ProjectExplorer.BuildSteps.Clean
211 |
212 | 2
213 | false
214 |
215 | false
216 |
217 | Profile
218 | Qt4ProjectManager.Qt4BuildConfiguration
219 | 0
220 | 0
221 | 0
222 |
223 | 3
224 |
225 |
226 | 0
227 | 部署
228 | 部署
229 | ProjectExplorer.BuildSteps.Deploy
230 |
231 | 1
232 |
233 | false
234 | ProjectExplorer.DefaultDeployConfiguration
235 |
236 | 1
237 |
238 | true
239 | true
240 | 0
241 | true
242 |
243 | 2
244 |
245 | false
246 | -e cpu-cycles --call-graph "dwarf,4096" -F 250
247 |
248 | Qt4ProjectManager.Qt4RunConfiguration:
249 | F:/newChar/NewCharClient/NewCharClient.pro
250 | false
251 | true
252 | true
253 | true
254 | F:/newChar/NewCharClient/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug
255 |
256 | 1
257 |
258 |
259 |
260 | ProjectExplorer.Project.TargetCount
261 | 1
262 |
263 |
264 | ProjectExplorer.Project.Updater.FileVersion
265 | 22
266 |
267 |
268 | Version
269 | 22
270 |
271 |
272 |
--------------------------------------------------------------------------------
/Client/addfriend.cpp:
--------------------------------------------------------------------------------
1 | #include "addfriend.h"
2 | #include "ui_addfriend.h"
3 | #include
4 |
5 | AddFriend::AddFriend(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::AddFriend)
8 | {
9 | ui->setupUi(this);
10 | this->setWindowFlags(Qt::FramelessWindowHint);
11 | }
12 |
13 | AddFriend::~AddFriend()
14 | {
15 | delete ui;
16 | }
17 |
18 |
19 | void AddFriend::initwidget(int id, QString name, QPixmap pixmap)
20 | {
21 | pixmap = pixmap.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
22 | QPixmap circularPixmap(ui->label->size());
23 | circularPixmap.fill(Qt::transparent);
24 | QPainter painter(&circularPixmap);
25 | painter.setRenderHint(QPainter::Antialiasing);
26 | painter.setBrush(QBrush(pixmap));
27 | painter.setPen(Qt::NoPen);
28 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
29 | ui->label->setPixmap(circularPixmap);
30 | ui->label->setAlignment(Qt::AlignCenter);
31 | this->id=id;
32 | ui->label_2->setText(name);
33 | }
34 |
35 |
36 | void AddFriend::on_pushButton_clicked()
37 | {
38 | emit addfriend(id);
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/Client/addfriend.h:
--------------------------------------------------------------------------------
1 | #ifndef ADDFRIEND_H
2 | #define ADDFRIEND_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class AddFriend;
8 | }
9 |
10 | class AddFriend : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | int id;
16 | explicit AddFriend(QWidget *parent = nullptr);
17 | ~AddFriend();
18 |
19 | public slots:
20 | void initwidget(int id,QString name,QPixmap pixmap);
21 |
22 | private slots:
23 | void on_pushButton_clicked();
24 |
25 | private:
26 | Ui::AddFriend *ui;
27 | public:signals:
28 | void addfriend(int id);
29 | };
30 |
31 | #endif // ADDFRIEND_H
32 |
--------------------------------------------------------------------------------
/Client/addfriend.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | AddFriend
4 |
5 |
6 |
7 | 0
8 | 0
9 | 400
10 | 80
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 50
22 | 50
23 |
24 |
25 |
26 |
27 | 50
28 | 50
29 |
30 |
31 |
32 | background-color: rgba(255, 255, 255, 0);
33 |
34 |
35 |
36 |
37 |
38 |
39 | -
40 |
41 |
42 | background-color: rgba(255, 255, 255, 0);
43 |
44 |
45 | 111111111
46 |
47 |
48 |
49 | -
50 |
51 |
52 | Qt::Horizontal
53 |
54 |
55 |
56 | 205
57 | 20
58 |
59 |
60 |
61 |
62 | -
63 |
64 |
65 |
66 | 70
67 | 0
68 |
69 |
70 |
71 |
72 | 70
73 | 20
74 |
75 |
76 |
77 | border-radius:3px;
78 | background-color: rgb(85, 170, 255);
79 |
80 |
81 | 添加为好友
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
--------------------------------------------------------------------------------
/Client/attnitem.cpp:
--------------------------------------------------------------------------------
1 | #include "attnitem.h"
2 | #include "ui_attnitem.h"
3 | #include
4 |
5 | AttnItem::AttnItem(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::AttnItem)
8 | {
9 | ui->setupUi(this);
10 | ui->pushButton->setStyleSheet(
11 | "QPushButton{"
12 | "border-radius:3px;"
13 | "background-color: rgb(85, 170, 255);"
14 | "color: rgb(0, 0, 0);}"
15 | "QPushButton:hover {"
16 | "background-color: rgb(50, 131, 231);"
17 | "border-radius:3px;"
18 | "color: rgb(0, 0, 0);"
19 | "}"
20 | );
21 | ui->pushButton_2->setStyleSheet(
22 | "QPushButton{"
23 | "border-radius:3px;"
24 | "background-color: rgb(204, 0, 0);"
25 | "color: rgb(0, 0, 0);}"
26 | "QPushButton:hover {"
27 | "background-color: rgb(160, 0, 0);"
28 | "border-radius:3px;"
29 | "color: rgb(0, 0, 0);"
30 | "}"
31 | );
32 | }
33 |
34 | AttnItem::~AttnItem()
35 | {
36 | delete ui;
37 | }
38 |
39 |
40 | void AttnItem::init(int id, QString name, QPixmap pix)
41 | {
42 | ui->label_2->setText(name+"请求添加好友");
43 | this->name=name;
44 | this->id=id;
45 | QPixmap pixmap = pix.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
46 | QPixmap circularPixmap(ui->label->size());
47 | circularPixmap.fill(Qt::transparent);
48 | QPainter painter(&circularPixmap);
49 | painter.setRenderHint(QPainter::Antialiasing);
50 | painter.setBrush(QBrush(pixmap));
51 | painter.setPen(Qt::NoPen);
52 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
53 | ui->label->setPixmap(circularPixmap);
54 | ui->label->setAlignment(Qt::AlignCenter);
55 | this->pixmap=pix;
56 | }
57 |
58 |
59 | void AttnItem::on_pushButton_clicked()//点击同意
60 | {
61 | agreeclicked(this->id,this->name,this->pixmap);
62 | }
63 |
64 |
65 | void AttnItem::on_pushButton_2_clicked()//不同意
66 | {
67 | emit disagree(this->id);
68 | }
69 |
70 |
--------------------------------------------------------------------------------
/Client/attnitem.h:
--------------------------------------------------------------------------------
1 | #ifndef ATTNITEM_H
2 | #define ATTNITEM_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class AttnItem;
8 | }
9 |
10 | class AttnItem : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit AttnItem(QWidget *parent = nullptr);
16 | ~AttnItem();
17 |
18 | private:
19 | int id;
20 | QString name;
21 | QPixmap pixmap;
22 |
23 | public:
24 | void init(int id,QString name,QPixmap pix);
25 |
26 | private slots:
27 | void on_pushButton_clicked();
28 |
29 | void on_pushButton_2_clicked();
30 |
31 | private:
32 | Ui::AttnItem *ui;
33 |
34 | public:signals:
35 | void agreeclicked(int id,QString name,QPixmap pixmap);
36 | void disagree(int id);
37 | };
38 |
39 | #endif // ATTNITEM_H
40 |
--------------------------------------------------------------------------------
/Client/attnitem.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | AttnItem
4 |
5 |
6 |
7 | 0
8 | 0
9 | 400
10 | 80
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 50
22 | 50
23 |
24 |
25 |
26 |
27 | 50
28 | 50
29 |
30 |
31 |
32 | background-color: rgba(255, 255, 255, 0);
33 |
34 |
35 |
36 |
37 |
38 |
39 | -
40 |
41 |
42 | background-color: rgba(255, 255, 255, 0);
43 | color: rgb(0, 0, 0);
44 |
45 |
46 | 11111111111
47 |
48 |
49 |
50 | -
51 |
52 |
53 | Qt::Horizontal
54 |
55 |
56 |
57 | 181
58 | 20
59 |
60 |
61 |
62 |
63 | -
64 |
65 |
66 |
67 | 40
68 | 20
69 |
70 |
71 |
72 |
73 | 40
74 | 20
75 |
76 |
77 |
78 | border-radius:3px;
79 | background-color: rgb(85, 170, 255);
80 | color: rgb(0, 0, 0);
81 |
82 |
83 | 同意
84 |
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 40
92 | 20
93 |
94 |
95 |
96 |
97 | 40
98 | 20
99 |
100 |
101 |
102 | color: rgb(0, 0, 0);
103 | background-color: rgb(204, 0, 0);
104 | border-radius:3px;
105 |
106 |
107 | 不同意
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/Client/chat/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Client/chatplaintext.cpp:
--------------------------------------------------------------------------------
1 | #include "chatplaintext.h"
2 | #include "ui_chatplaintext.h"
3 | #include
4 | #include "stickerwidget.h"
5 | #include
6 | #include
7 | #include
8 |
9 | ChatPlainText::ChatPlainText(QWidget *parent)
10 | : QWidget(parent)
11 | , ui(new Ui::ChatPlainText)
12 | {
13 | ui->setupUi(this);
14 | this->setWindowFlags(Qt::FramelessWindowHint);
15 | ui->pushButton_4->setStyleSheet(
16 | "QPushButton{"
17 | "background-color: rgb(85, 170, 255);"
18 | "color: rgb(255, 255, 255);"
19 | "border-radius:4px;}"
20 | "QPushButton:hover {"
21 | "background-color: rgb(50, 131, 231);"
22 | "color: rgb(255, 255, 255);"
23 | "border-radius:4px;"
24 | "}"
25 | );
26 | ui->widget->setStyleSheet(
27 | "border: 0px;"
28 | "border-top: 1px dotted #bebebe;"
29 | );
30 | }
31 |
32 | ChatPlainText::~ChatPlainText()
33 | {
34 | delete ui;
35 | }
36 |
37 | void ChatPlainText::on_pushButton_4_clicked()
38 | {
39 | QTextDocument* document = ui->textEdit->document();
40 | QTextFrame* rootFrame = document->rootFrame();
41 | QTextFrame::iterator it;
42 | for (it = rootFrame->begin(); !(it.atEnd()); ++it) {
43 | QTextBlock block = it.currentBlock();
44 | if (!block.isValid()) continue;
45 | QTextBlock::iterator fragmentIt;
46 | for (fragmentIt = block.begin(); !(fragmentIt.atEnd()); ++fragmentIt) {
47 | QTextFragment fragment = fragmentIt.fragment();
48 | if (fragment.charFormat().isImageFormat()) {
49 | QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
50 | QString imageName = imageFormat.property(QTextFormat::UserProperty).toString();
51 | QString emoji;
52 | if(imageName==":/icon/35.png"){
53 | emoji="😎";
54 | }else if(imageName==":/icon/36.png"){
55 | emoji="😙";
56 | }else if(imageName==":/icon/37.png"){
57 | emoji="😭";
58 | }else if(imageName==":/icon/38.png"){
59 | emoji="😁";
60 | }else if(imageName==":/icon/41.png"){
61 | emoji="😟";
62 | }else if(imageName==":/icon/40.png"){
63 | emoji="🤬";
64 | }
65 | QTextCursor cursor = ui->textEdit->textCursor();
66 | cursor.setPosition(fragment.position());
67 | cursor.setPosition(fragment.position() + fragment.length(), QTextCursor::KeepAnchor);
68 | cursor.insertText(emoji);
69 | }
70 | }
71 | }
72 | QString msg=ui->textEdit->toPlainText();
73 | qDebug()<<"msg:"<textEdit->clear();
76 | }
77 |
78 |
79 | void ChatPlainText::on_pushButton_2_clicked()
80 | {
81 | emit sendfile();
82 | }
83 |
84 |
85 | void ChatPlainText::on_pushButton_3_clicked()
86 | {
87 | emit sendpicture();
88 | }
89 |
90 |
91 | void ChatPlainText::on_pushButton_clicked()
92 | {
93 | Stickerwidget *sticker=new Stickerwidget;
94 | connect(sticker,&Stickerwidget::sticker1,this,[=](){
95 | on_sticker(QPixmap(":/icon/35.png"),":/icon/35.png");
96 | });
97 | connect(sticker,&Stickerwidget::sticker2,this,[=](){
98 | on_sticker(QPixmap(":/icon/36.png"),":/icon/36.png");
99 | });
100 | connect(sticker,&Stickerwidget::sticker3,this,[=](){
101 | on_sticker(QPixmap(":/icon/37.png"),":/icon/37.png");
102 | });
103 | connect(sticker,&Stickerwidget::sticker4,this,[=](){
104 | on_sticker(QPixmap(":/icon/38.png"),":/icon/38.png");
105 | });
106 | connect(sticker,&Stickerwidget::sticker5,this,[=](){
107 | on_sticker(QPixmap(":/icon/41.png"),":/icon/41.png");
108 | });
109 | connect(sticker,&Stickerwidget::sticker6,this,[=](){
110 | on_sticker(QPixmap(":/icon/40.png"),":/icon/40.png");
111 | });
112 | connect(sticker,&Stickerwidget::focusOut,this,[=](){
113 | sticker->deleteLater();
114 | });
115 | QPoint globalPos = ui->pushButton->mapToGlobal(QPoint(0,0));
116 | int x = globalPos.x() + (ui->pushButton->width() - sticker->width()) / 2;
117 | int y = globalPos.y() - sticker->height() - 5;
118 | sticker->move(x, y);
119 | sticker->show();
120 | sticker->setFocus();
121 | }
122 |
123 | void ChatPlainText::on_sticker(QPixmap pix,QString path)
124 | {
125 | QImage image = pix.scaled(15, 15, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).toImage();
126 | QString resourceName = QFileInfo(path).fileName();
127 | ui->textEdit->document()->addResource(QTextDocument::ImageResource, QUrl(resourceName), image);
128 | QTextImageFormat imgFormat;
129 | imgFormat.setName(resourceName);
130 | imgFormat.setWidth(15);
131 | imgFormat.setHeight(15);
132 | imgFormat.setProperty(QTextFormat::UserProperty, path);
133 | QTextCursor cursor = ui->textEdit->textCursor();
134 | cursor.insertImage(imgFormat);
135 | }
136 |
137 |
138 |
--------------------------------------------------------------------------------
/Client/chatplaintext.h:
--------------------------------------------------------------------------------
1 | #ifndef CHATPLAINTEXT_H
2 | #define CHATPLAINTEXT_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | namespace Ui {
9 | class ChatPlainText;
10 | }
11 |
12 | class ChatPlainText : public QWidget
13 | {
14 | Q_OBJECT
15 |
16 | public:
17 | explicit ChatPlainText(QWidget *parent = nullptr);
18 | ~ChatPlainText();
19 |
20 |
21 | private slots:
22 | void on_pushButton_4_clicked();
23 |
24 | void on_pushButton_2_clicked();
25 |
26 | void on_pushButton_3_clicked();
27 |
28 | void on_pushButton_clicked();
29 |
30 | void on_sticker(QPixmap pix,QString path);
31 |
32 |
33 |
34 | private:
35 | QHash m_imageIdToPath;
36 | Ui::ChatPlainText *ui;
37 |
38 | public:signals:
39 | void sendmsg(QString msg);
40 | void sendfile();
41 | void sendpicture();
42 | };
43 |
44 | #endif // CHATPLAINTEXT_H
45 |
--------------------------------------------------------------------------------
/Client/chatplaintext.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | ChatPlainText
4 |
5 |
6 |
7 | 0
8 | 0
9 | 400
10 | 300
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 | 0
19 |
20 |
21 | 0
22 |
23 |
24 | 0
25 |
26 |
27 | 0
28 |
29 |
30 | 0
31 |
32 | -
33 |
34 |
35 |
36 |
37 |
38 |
39 | 20
40 |
41 |
42 | 0
43 |
44 |
-
45 |
46 |
47 |
48 | 30
49 | 30
50 |
51 |
52 |
53 |
54 | 30
55 | 30
56 |
57 |
58 |
59 | background-color: rgba(255, 255, 255, 0);
60 |
61 |
62 |
63 |
64 |
65 |
66 | :/icon/16.png:/icon/16.png
67 |
68 |
69 |
70 | 25
71 | 25
72 |
73 |
74 |
75 |
76 | -
77 |
78 |
79 |
80 | 30
81 | 30
82 |
83 |
84 |
85 |
86 | 30
87 | 30
88 |
89 |
90 |
91 | background-color: rgba(255, 255, 255, 0);
92 |
93 |
94 |
95 |
96 |
97 |
98 | :/icon/17.png:/icon/17.png
99 |
100 |
101 |
102 | 25
103 | 25
104 |
105 |
106 |
107 |
108 | -
109 |
110 |
111 |
112 | 30
113 | 30
114 |
115 |
116 |
117 |
118 | 30
119 | 30
120 |
121 |
122 |
123 | background-color: rgba(255, 255, 255, 0);
124 |
125 |
126 |
127 |
128 |
129 |
130 | :/icon/18.png:/icon/18.png
131 |
132 |
133 |
134 | 25
135 | 25
136 |
137 |
138 |
139 |
140 | -
141 |
142 |
143 | Qt::Horizontal
144 |
145 |
146 |
147 | 223
148 | 20
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 | -
157 |
158 |
159 | border: none;
160 | font: 12pt "Microsoft YaHei UI";
161 |
162 |
163 |
164 | -
165 |
166 |
167 |
-
168 |
169 |
170 | Qt::Horizontal
171 |
172 |
173 |
174 | 289
175 | 20
176 |
177 |
178 |
179 |
180 | -
181 |
182 |
183 |
184 | 80
185 | 25
186 |
187 |
188 |
189 |
190 | 80
191 | 26
192 |
193 |
194 |
195 | border-radius:4px;
196 | color: rgb(255, 255, 255);
197 | background-color: rgb(85, 170, 255);
198 |
199 |
200 | 发送
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
--------------------------------------------------------------------------------
/Client/form.cpp:
--------------------------------------------------------------------------------
1 | #include "form.h"
2 | #include "ui_form.h"
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | Form::Form(QWidget *parent)
9 | : QWidget(parent)
10 | , ui(new Ui::Form)
11 | {
12 | ui->setupUi(this);
13 | init();
14 | home=new Home;
15 | home->hide();
16 | regither=new Register;
17 | regither->hide();
18 |
19 | subthread=new QThread;
20 | senddata=new SendData;
21 | senddata->moveToThread(subthread);
22 | connect(this,&Form::senddatainit,senddata,&SendData::init);
23 | connect(this,&Form::userlogon,senddata,&SendData::on_userlogon);//用户登录的槽函数
24 | connect(regither,&Register::login,senddata,&SendData::on_login);//用户注册
25 | connect(home,&Home::updateinformation,senddata,&SendData::on_updateinformation);//更新信息
26 | connect(home,&Home::updatenameonly,senddata,&SendData::on_updatenameonly);//只更新名字
27 | subthread->start();
28 |
29 | emit senddatainit();
30 |
31 | connect(senddata,&SendData::logonfailure,this,[=](){
32 | QMessageBox::warning(this,"注意","账号或密码错误");
33 | });
34 | connect(senddata,&SendData::logoning,this,[=](){
35 | QMessageBox::warning(this,"注意","此账号正在其他设备登录");
36 | });
37 | connect(senddata,&SendData::logonsuccsee,home,&Home::on_logonsuccsee);//设置home的id和名字
38 | connect(senddata,&SendData::logonsuccseeicon,this,[=](QByteArray data){//设置home界面头像
39 | home->on_logonsuccseeicon(data);
40 | home->show();
41 | regither->close();
42 | this->close();
43 | });
44 | connect(senddata,&SendData::loginfailure,this,[=](){
45 | QMessageBox::warning(this,"注意","注册失败,请重新尝试");
46 | });
47 | connect(senddata,&SendData::loginsuccsee,home,&Home::on_logonsuccsee);//用户注册设置home的id和名字
48 | connect(senddata,&SendData::loginsuccseeicon,this,[=](QByteArray data){//用户注册设置home界面头像
49 | home->on_logonsuccseeicon(data);
50 | home->show();
51 | regither->close();
52 | this->close();
53 | });
54 | connect(senddata,&SendData::initfriend,this,[=](int friendid,QString friendname,QByteArray data){
55 | QPixmap pix;
56 | pix.loadFromData(data);
57 | home->friendwidgetadd(friendid,friendname,pix);
58 | });
59 | connect(home,&Home::selectfriendid,senddata,&SendData::on_selectfriendid);//通过id查询用户
60 | connect(senddata,&SendData::isnullid,home,&Home::on_isnullid);//查询的id为空
61 | connect(senddata,&SendData::showselectuser,this,[=](int friendid,QString friendname,QByteArray data){
62 | QPixmap pix;
63 | pix.loadFromData(data);
64 | home->selectlistadd(friendid,friendname,pix);
65 | });
66 | connect(home,&Home::addfriendrequest,senddata,&SendData::on_addfriendrequest);//提示有好友申请
67 | connect(senddata,&SendData::showfriendrequest,this,[=](int friendid,QString friendname,QByteArray data){
68 | QPixmap pix;
69 | pix.loadFromData(data);
70 | home->on_addfriendrequest(friendid,friendname,pix);
71 | });
72 | connect(home,&Home::agreeaddfriend,senddata,&SendData::on_agreeaddfriend);//同意添加好友
73 | connect(home,&Home::sendtofriend,senddata,&SendData::on_sendtofriend);//发普通消息
74 | connect(senddata,&SendData::receivesendmsg,home,&Home::on_receivesendmsg);//收到别人发来的消息
75 | connect(home,&Home::sendfiletofriend,senddata,&SendData::on_sendfiletofriend);//发文件
76 | connect(senddata,&SendData::mysendsize,home,&Home::on_mysendsize);//设置自己传输文件进度条
77 | connect(senddata,&SendData::frinedsendfile,home,&Home::on_frinedsendfile);//收到好友发文件的信号创建对方发文件的widget
78 | connect(senddata,&SendData::friendsendfilesize,home,&Home::on_friendsendfilesize);//设置接收文件进度条
79 | connect(home,&Home::sendpicture,senddata,&SendData::on_sendpicture);//发送图片
80 | connect(senddata,&SendData::friendsendpicture,home,&Home::on_friendsendpicture);//收到朋友发来的图片
81 | connect(senddata,&SendData::friendpicturechanged,home,&Home::on_friendpicturechanged);//收到朋友改变头像
82 | connect(senddata,&SendData::friendnamechanged,home,&Home::on_friendnamechanged);//收到好友改变昵称的信号槽
83 | connect(home,&Home::closehome,senddata,&SendData::on_closehome);//关闭程序信号槽
84 | connect(senddata,&SendData::deletesocket,this,[=](){//关闭窗口,释放资源
85 | close_num++;
86 | if(close_num>=2){
87 | subthread->quit();
88 | subthread->wait();
89 | subthread->deleteLater();
90 | senddata->deleteLater();
91 | }
92 | });
93 | connect(regither,&Register::closewidget,this,[=](){
94 | regither->hide();
95 | });
96 |
97 | }
98 |
99 | Form::~Form()
100 | {
101 | delete regither;
102 | delete home;
103 | delete ui;
104 | }
105 |
106 | void Form::init()
107 | {
108 | setWindowFlags(Qt::FramelessWindowHint);
109 | setAttribute(Qt::WA_TranslucentBackground);
110 |
111 |
112 | ui->pushButton_3->setStyleSheet(
113 | "QPushButton{"
114 | "border-image: url(:/icon/1.png);}"
115 | "QPushButton:hover {"
116 | "border-image: url(:/icon/2.png);"
117 | "}"
118 | );
119 | ui->pushButton->setStyleSheet(
120 | "QPushButton{"
121 | "background-color: rgb(85, 170, 255);"
122 | "font: 14pt \"Microsoft YaHei UI\";"
123 | "color: rgb(255, 255, 255);"
124 | "border-radius:6px;}"
125 | "QPushButton:hover {"
126 | "background-color: rgb(50, 131, 231);"
127 | "font: 14pt \"Microsoft YaHei UI\";"
128 | "color: rgb(255, 255, 255);"
129 | "border-radius:6px;"
130 | "}"
131 | );
132 | ui->label->setStyleSheet(
133 | "border-radius: 50%;"
134 | "background-color: transparent;"
135 | );
136 | QPixmap pixmap(":/icon/3.png");
137 | pixmap = pixmap.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
138 | QPixmap circularPixmap(ui->label->size());
139 | circularPixmap.fill(Qt::transparent);
140 | QPainter painter(&circularPixmap);
141 | painter.setRenderHint(QPainter::Antialiasing);
142 | painter.setBrush(QBrush(pixmap));
143 | painter.setPen(Qt::NoPen);
144 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
145 | ui->label->setPixmap(circularPixmap);
146 | ui->label->setAlignment(Qt::AlignCenter);
147 | }
148 |
149 | void Form::paintEvent(QPaintEvent *event)
150 | {
151 | QPainter painter(this);
152 | painter.setRenderHint(QPainter::Antialiasing);
153 | painter.setBrush(QColor(240,240,240));
154 | painter.setPen(Qt::NoPen);
155 | painter.drawRoundedRect(rect(), 15, 15);
156 | }
157 |
158 | void Form::on_pushButton_3_clicked()
159 | {
160 | this->close();
161 | }
162 |
163 |
164 | void Form::on_pushButton_clicked()//点击登录
165 | {
166 | int id=ui->lineEdit->text().toInt();
167 | QString password=ui->lineEdit_2->text();
168 | emit userlogon(id,password);
169 | }
170 |
171 |
172 | void Form::on_pushButton_2_clicked()
173 | {
174 | regither->show();
175 | }
176 |
177 |
--------------------------------------------------------------------------------
/Client/form.h:
--------------------------------------------------------------------------------
1 | #ifndef FORM_H
2 | #define FORM_H
3 |
4 | #include
5 | #include"senddata.h"
6 | #include
7 | #include "home.h"
8 | #include "register.h"
9 |
10 | namespace Ui {
11 | class Form;
12 | }
13 |
14 | class Form : public QWidget
15 | {
16 | Q_OBJECT
17 |
18 | public:
19 | explicit Form(QWidget *parent = nullptr);
20 | ~Form();
21 |
22 | private:
23 | void init();
24 |
25 | protected:
26 | void paintEvent(QPaintEvent *event)override;
27 |
28 | private slots:
29 | void on_pushButton_3_clicked();
30 |
31 | void on_pushButton_clicked();
32 |
33 | void on_pushButton_2_clicked();
34 |
35 | private:
36 | SendData *senddata=NULL;
37 | QThread *subthread=NULL;
38 | Home *home=NULL;
39 | Register *regither=NULL;
40 | int close_num=0;
41 |
42 | private:
43 | Ui::Form *ui;
44 |
45 | public:signals:
46 | void senddatainit();
47 | void userlogon(int id,QString password);
48 | };
49 |
50 | #endif // FORM_H
51 |
--------------------------------------------------------------------------------
/Client/form.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Form
4 |
5 |
6 |
7 | 0
8 | 0
9 | 325
10 | 438
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 | 0
19 |
20 |
21 | 0
22 |
23 |
24 | 0
25 |
26 |
27 | 0
28 |
29 |
30 | 0
31 |
32 | -
33 |
34 |
35 | 10
36 |
37 |
-
38 |
39 |
40 |
-
41 |
42 |
43 | Qt::Horizontal
44 |
45 |
46 |
47 | 289
48 | 20
49 |
50 |
51 |
52 |
53 | -
54 |
55 |
56 |
57 | 15
58 | 15
59 |
60 |
61 |
62 |
63 | 15
64 | 15
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | -
76 |
77 |
78 | Qt::Vertical
79 |
80 |
81 |
82 | 20
83 | 85
84 |
85 |
86 |
87 |
88 | -
89 |
90 |
-
91 |
92 |
93 | Qt::Horizontal
94 |
95 |
96 |
97 | 40
98 | 20
99 |
100 |
101 |
102 |
103 | -
104 |
105 |
106 |
107 | 70
108 | 70
109 |
110 |
111 |
112 |
113 | 70
114 | 70
115 |
116 |
117 |
118 | background-color: rgba(255, 255, 255, 0);
119 |
120 |
121 |
122 |
123 |
124 |
125 | -
126 |
127 |
128 | Qt::Horizontal
129 |
130 |
131 |
132 | 40
133 | 20
134 |
135 |
136 |
137 |
138 |
139 |
140 | -
141 |
142 |
-
143 |
144 |
145 | Qt::Horizontal
146 |
147 |
148 |
149 | 40
150 | 20
151 |
152 |
153 |
154 |
155 | -
156 |
157 |
158 |
159 | 250
160 | 40
161 |
162 |
163 |
164 | border-radius:8px;
165 | background-color: rgb(255, 255, 255);
166 | font: 16pt "Microsoft YaHei UI";
167 |
168 |
169 | Qt::AlignCenter
170 |
171 |
172 | 输入账号
173 |
174 |
175 |
176 | -
177 |
178 |
179 | Qt::Horizontal
180 |
181 |
182 |
183 | 40
184 | 20
185 |
186 |
187 |
188 |
189 |
190 |
191 | -
192 |
193 |
-
194 |
195 |
196 | Qt::Horizontal
197 |
198 |
199 |
200 | 40
201 | 20
202 |
203 |
204 |
205 |
206 | -
207 |
208 |
209 |
210 | 250
211 | 40
212 |
213 |
214 |
215 | border-radius:8px;
216 | background-color: rgb(255, 255, 255);
217 | font: 16pt "Microsoft YaHei UI";
218 |
219 |
220 |
221 |
222 |
223 | QLineEdit::Password
224 |
225 |
226 | Qt::AlignCenter
227 |
228 |
229 | 输入密码
230 |
231 |
232 |
233 | -
234 |
235 |
236 | Qt::Horizontal
237 |
238 |
239 |
240 | 40
241 | 20
242 |
243 |
244 |
245 |
246 |
247 |
248 | -
249 |
250 |
251 | Qt::Vertical
252 |
253 |
254 |
255 | 20
256 | 40
257 |
258 |
259 |
260 |
261 | -
262 |
263 |
-
264 |
265 |
266 | Qt::Horizontal
267 |
268 |
269 |
270 | 40
271 | 20
272 |
273 |
274 |
275 |
276 | -
277 |
278 |
279 |
280 | 250
281 | 30
282 |
283 |
284 |
285 |
286 | 200
287 | 30
288 |
289 |
290 |
291 | background-color: rgb(85, 170, 255);
292 | font: 14pt "Microsoft YaHei UI";
293 | color: rgb(255, 255, 255);
294 | border-radius:6px;
295 |
296 |
297 | 登录
298 |
299 |
300 |
301 | -
302 |
303 |
304 | Qt::Horizontal
305 |
306 |
307 |
308 | 40
309 | 20
310 |
311 |
312 |
313 |
314 |
315 |
316 | -
317 |
318 |
319 | background-color: rgba(255, 255, 255, 0);
320 |
321 |
322 | 没有账号,去注册
323 |
324 |
325 |
326 | -
327 |
328 |
329 | Qt::Vertical
330 |
331 |
332 |
333 | 20
334 | 85
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
--------------------------------------------------------------------------------
/Client/frienditem.cpp:
--------------------------------------------------------------------------------
1 | #include "frienditem.h"
2 | #include "ui_frienditem.h"
3 | #include
4 |
5 | FriendItem::FriendItem(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::FriendItem)
8 | {
9 | ui->setupUi(this);
10 | }
11 |
12 | FriendItem::~FriendItem()
13 | {
14 | delete ui;
15 | }
16 | void FriendItem::init(int id, QString name, QPixmap pix)
17 | {
18 | this->id=id;
19 | this->name=name;
20 | m_pix=pix;
21 | ui->label_2->setText(name);
22 | QPixmap pixmap = pix.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
23 | QPixmap circularPixmap(ui->label->size());
24 | circularPixmap.fill(Qt::transparent);
25 | QPainter painter(&circularPixmap);
26 | painter.setRenderHint(QPainter::Antialiasing);
27 | painter.setBrush(QBrush(pixmap));
28 | painter.setPen(Qt::NoPen);
29 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
30 | ui->label->setPixmap(circularPixmap);
31 | ui->label->setAlignment(Qt::AlignCenter);
32 |
33 | }
34 |
35 | int FriendItem::getid()
36 | {
37 | return this->id;
38 | }
39 |
40 | QString FriendItem::getname()
41 | {
42 | return this->name;
43 | }
44 |
45 | QPixmap FriendItem::getpixmap()
46 | {
47 | return this->m_pix;
48 | }
49 |
50 | void FriendItem::havemsg(bool isok)
51 | {
52 | if(isok){
53 | ui->label_3->setPixmap(QPixmap(":/icon/19.png"));
54 | }else{
55 | QPixmap pix;
56 | ui->label_3->setPixmap(pix);
57 | }
58 | }
59 |
60 | void FriendItem::setmsg(QString msg, QString curtime)
61 | {
62 | ui->label_4->setText(msg);
63 | ui->label_5->setText(curtime);
64 | }
65 |
66 | void FriendItem::picturechanged(QPixmap pix)
67 | {
68 | m_pix=pix;
69 | QPixmap pixmap = pix.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
70 | QPixmap circularPixmap(ui->label->size());
71 | circularPixmap.fill(Qt::transparent);
72 | QPainter painter(&circularPixmap);
73 | painter.setRenderHint(QPainter::Antialiasing);
74 | painter.setBrush(QBrush(pixmap));
75 | painter.setPen(Qt::NoPen);
76 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
77 | ui->label->setPixmap(circularPixmap);
78 | ui->label->setAlignment(Qt::AlignCenter);
79 | }
80 |
81 | void FriendItem::namechanged(QString name)
82 | {
83 | ui->label_2->setText(name);
84 | }
85 |
--------------------------------------------------------------------------------
/Client/frienditem.h:
--------------------------------------------------------------------------------
1 | #ifndef FRIENDITEM_H
2 | #define FRIENDITEM_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class FriendItem;
8 | }
9 |
10 | class FriendItem : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit FriendItem(QWidget *parent = nullptr);
16 | ~FriendItem();
17 | int id;
18 | QString name;
19 | QPixmap m_pix;
20 |
21 | public:
22 | void init(int id,QString name,QPixmap pix);
23 | int getid();
24 | QString getname();
25 | QPixmap getpixmap();
26 | void havemsg(bool isok);
27 | void setmsg(QString msg,QString curtime);
28 | void picturechanged(QPixmap pix);
29 | void namechanged(QString name);
30 | private:
31 | Ui::FriendItem *ui;
32 | };
33 |
34 | #endif // FRIENDITEM_H
35 |
--------------------------------------------------------------------------------
/Client/frienditem.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | FriendItem
4 |
5 |
6 |
7 | 0
8 | 0
9 | 500
10 | 80
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 50
22 | 50
23 |
24 |
25 |
26 |
27 | 50
28 | 50
29 |
30 |
31 |
32 | background-color: rgba(255, 255, 255, 0);
33 |
34 |
35 |
36 |
37 |
38 |
39 | -
40 |
41 |
42 | 0
43 |
44 |
-
45 |
46 |
47 |
48 | 400
49 | 0
50 |
51 |
52 |
53 | background-color: rgba(255, 255, 255, 0);
54 | font: 10pt "Microsoft YaHei UI";
55 | color: rgb(0, 0, 0);
56 |
57 |
58 | 111
59 |
60 |
61 |
62 | -
63 |
64 |
65 | color: rgb(85, 85, 85);
66 | background-color: rgba(255, 255, 255, 0);
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | -
76 |
77 |
-
78 |
79 |
80 |
81 | 40
82 | 16777215
83 |
84 |
85 |
86 | color: rgb(85, 85, 85);
87 | background-color: rgba(255, 255, 255, 0);
88 |
89 |
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 | Qt::Vertical
98 |
99 |
100 |
101 | 20
102 | 17
103 |
104 |
105 |
106 |
107 | -
108 |
109 |
-
110 |
111 |
112 | Qt::Horizontal
113 |
114 |
115 |
116 | 40
117 | 20
118 |
119 |
120 |
121 |
122 | -
123 |
124 |
125 |
126 | 10
127 | 10
128 |
129 |
130 |
131 |
132 | 10
133 | 10
134 |
135 |
136 |
137 | background-color: rgba(255, 255, 255, 0);
138 |
139 |
140 |
141 |
142 |
143 | true
144 |
145 |
146 |
147 |
148 |
149 | -
150 |
151 |
152 | Qt::Vertical
153 |
154 |
155 |
156 | 20
157 | 17
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
--------------------------------------------------------------------------------
/Client/friendsendfile.cpp:
--------------------------------------------------------------------------------
1 | #include "friendsendfile.h"
2 | #include "ui_friendsendfile.h"
3 |
4 | FriendSendfile::FriendSendfile(QWidget *parent)
5 | : QWidget(parent)
6 | , ui(new Ui::FriendSendfile)
7 | {
8 | ui->setupUi(this);
9 | }
10 |
11 | FriendSendfile::~FriendSendfile()
12 | {
13 | delete ui;
14 | }
15 |
16 | void FriendSendfile::setfile(QString filename, qint64 size,QString suffix)
17 | {
18 | ui->label_2->setText(filename);
19 | double sizeKB=size/1024.0;
20 | if(sizeKB>=1024){
21 | double sizeMB=sizeKB/1024.0;
22 | if(sizeMB>=1024){
23 | double sizeGB=sizeMB/1024.0;
24 | ui->label_3->setText(QString::number(sizeGB)+"GB");
25 | }else{
26 | ui->label_3->setText(QString::number(sizeMB)+"MB");
27 | }
28 | }else{
29 | ui->label_3->setText(QString::number(sizeKB)+"KB");
30 | }
31 | QPixmap pix;
32 | if(suffix=="bmp"){
33 | pix=QPixmap(":/icon/20.png");
34 | }else if(suffix=="dos"){
35 | pix=QPixmap(":/icon/21.png");
36 | }else if(suffix=="eps"){
37 | pix=QPixmap(":/icon/22.png");
38 | }else if(suffix=="exe"){
39 | pix=QPixmap(":/icon/23.png");
40 | }else if(suffix=="html"){
41 | pix=QPixmap(":/icon/24.png");
42 | }else if(suffix=="mp4"){
43 | pix=QPixmap(":/icon/25.png");
44 | }else if(suffix=="pdf"){
45 | pix=QPixmap(":/icon/26.png");
46 | }else if(suffix=="ppt"){
47 | pix=QPixmap(":/icon/27.png");
48 | }else if(suffix=="psd"){
49 | pix=QPixmap(":/icon/28.png");
50 | }else if(suffix=="txt"){
51 | pix=QPixmap(":/icon/29.png");
52 | }else if(suffix=="xls"){
53 | pix=QPixmap(":/icon/30.png");
54 | }else if(suffix=="xml"){
55 | pix=QPixmap(":/icon/31.png");
56 | }else if(suffix=="zip"){
57 | pix=QPixmap(":/icon/34.png");
58 | }else if(suffix==""){
59 | pix=QPixmap(":/icon/33.png");
60 | }else{
61 | pix=QPixmap(":/icon/32.png");
62 | }
63 | ui->label->setText("");
64 | ui->label->setPixmap(pix);
65 | ui->label_4->setText("发送中");
66 | }
67 |
68 | void FriendSendfile::setvalue(int value)
69 | {
70 | ui->progressBar->setValue(value);
71 | if(value==100){
72 | ui->label_4->setText("发送完毕");
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Client/friendsendfile.h:
--------------------------------------------------------------------------------
1 | #ifndef FRIENDSENDFILE_H
2 | #define FRIENDSENDFILE_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class FriendSendfile;
8 | }
9 |
10 | class FriendSendfile : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit FriendSendfile(QWidget *parent = nullptr);
16 | ~FriendSendfile();
17 |
18 | void setfile(QString filename,qint64 size,QString suffix);
19 | void setvalue(int value);
20 |
21 | private:
22 | Ui::FriendSendfile *ui;
23 | };
24 |
25 | #endif // FRIENDSENDFILE_H
26 |
--------------------------------------------------------------------------------
/Client/friendsendfile.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | FriendSendfile
4 |
5 |
6 |
7 | 0
8 | 0
9 | 300
10 | 100
11 |
12 |
13 |
14 |
15 | 300
16 | 100
17 |
18 |
19 |
20 |
21 | 300
22 | 100
23 |
24 |
25 |
26 | Form
27 |
28 |
29 |
30 | 0
31 |
32 |
33 | 0
34 |
35 |
36 | 0
37 |
38 |
39 | 0
40 |
41 |
42 | 0
43 |
44 | -
45 |
46 |
47 |
48 | 300
49 | 100
50 |
51 |
52 |
53 |
54 | 300
55 | 100
56 |
57 |
58 |
59 | border-radius:10px;
60 | background-color: rgb(255, 255, 255);
61 |
62 |
63 |
-
64 |
65 |
-
66 |
67 |
68 |
69 | 50
70 | 60
71 |
72 |
73 |
74 |
75 | 50
76 | 60
77 |
78 |
79 |
80 | TextLabel
81 |
82 |
83 | true
84 |
85 |
86 |
87 | -
88 |
89 |
-
90 |
91 |
92 | color: rgb(0, 0, 0);
93 |
94 |
95 | TextLabel
96 |
97 |
98 |
99 | -
100 |
101 |
-
102 |
103 |
104 | color: rgb(0, 0, 0);
105 |
106 |
107 | TextLabel
108 |
109 |
110 |
111 | -
112 |
113 |
114 | Qt::Horizontal
115 |
116 |
117 |
118 | 40
119 | 20
120 |
121 |
122 |
123 |
124 | -
125 |
126 |
127 | color: rgb(0, 0, 0);
128 |
129 |
130 | TextLabel
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 | -
141 |
142 |
143 |
144 | 16777215
145 | 3
146 |
147 |
148 |
149 | 0
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
--------------------------------------------------------------------------------
/Client/home.h:
--------------------------------------------------------------------------------
1 | #ifndef HOME_H
2 | #define HOME_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include "prompt.h"
13 | #include "attnitem.h"
14 | #include "chatplaintext.h"
15 | #include "mysendfile.h"
16 | #include "friendsendfile.h"
17 |
18 |
19 | namespace Ui {
20 | class Home;
21 | }
22 |
23 | class Home : public QWidget
24 | {
25 | Q_OBJECT
26 |
27 | public:
28 | explicit Home(QWidget *parent = nullptr);
29 | ~Home();
30 |
31 | protected:
32 | void mousePressEvent(QMouseEvent *event)override;//鼠标按钮按下
33 | void mouseMoveEvent(QMouseEvent *event)override;//鼠标移动
34 | void mouseReleaseEvent(QMouseEvent *event) override;//鼠标按钮松开
35 | void closeEvent(QCloseEvent *event)override;
36 |
37 | private:
38 | int id;
39 | QString name;
40 | QPixmap pix;
41 | bool isleft=0;
42 | QPoint point;
43 | QAction *action;
44 | QListWidget *friendwidget=NULL,*attnwidget=NULL,*selectfriend=NULL;
45 | QVBoxLayout *layout1=NULL;
46 | QWidget *curlistwidget=NULL;//记录当前是哪个列表
47 | QString m_path;
48 | QMapchatmap;
49 | QVectorfriendvector;//记录当前有那些好友
50 | QVectorpromptvector;//记录当前有哪些好友申请
51 | QTimer timer;
52 | Prompt *prompt=NULL;
53 | ChatPlainText *chattext=NULL;
54 | QWidget *curchatscroll=NULL;
55 | int curfriendid;
56 | MySendfile *sendingfile=NULL;
57 | FriendSendfile *receivingfile=NULL;
58 |
59 | QHashEMOJI_MAP = {
60 | {"😎", ":/icon/35.png"},
61 | {"😙", ":/icon/36.png"},
62 | {"😭", ":/icon/37.png"},
63 | {"😁", ":/icon/38.png"},
64 | {"😟", ":/icon/41.png"},
65 | {"🤬", ":/icon/40.png"}
66 | };
67 |
68 | private:
69 | void widget_5addwidget(QListWidgetItem* item);
70 | void pushbuttoninit();
71 | bool promptdup(QVector*vec,int dupid);
72 | void widget_5clear();
73 | void deleteid(int id);
74 | QListWidgetItem *findItemByAttnItem(QListWidget *listWidget, AttnItem *targetAttnItem);
75 | QString replaceEmojisWithImages(const QString& msg);
76 |
77 | public slots:
78 | void on_logonsuccsee(int m_id,QString m_name);
79 | void on_logonsuccseeicon(QByteArray data);
80 | void friendwidgetadd(int id,QString name,QPixmap pix);
81 | void on_isnullid();
82 | void selectlistadd(int id, QString name, QPixmap pixmap);
83 | void on_addfriendrequest(int id, QString name, QPixmap pix);
84 | void on_receivesendmsg(int fromid,QString msg);
85 | void on_mysendsize(int value);
86 | void on_friendsendfilesize(int value);
87 | void on_frinedsendfile(qint64 size,QString filename,QString suffix,int fromid);
88 | void on_friendsendpicture(int fromid,QByteArray data,int h,int w);
89 | void on_friendpicturechanged(int friendid,QByteArray data);
90 | void on_friendnamechanged(int friendid ,QString friendname);
91 |
92 | private slots:
93 | void on_pushButton_4_clicked();
94 |
95 | void on_pushButton_2_clicked();
96 |
97 | void on_pushButton_clicked();
98 |
99 | void on_pushButton_5_clicked();
100 |
101 | void on_pushButton_6_clicked();
102 |
103 | void on_addfriend(int toid);
104 |
105 | void on_sendmsg(QString msg);
106 |
107 | void on_sendfile();
108 |
109 | void on_sendpicture();
110 |
111 |
112 | private:
113 | Ui::Home *ui;
114 | public:signals:
115 | void updateinformation(int id,QString name,QString path);
116 | void updatenameonly(int id,QString name);
117 | void selectfriendid(int id);
118 | void addfriendrequest(int id);
119 | void agreeaddfriend(int id);
120 | void sendtofriend(int toid,QString msg,int fromid);
121 | void sendfiletofriend(int toid,QString path);
122 | void sendpicture(QString path,int toid);
123 | void closehome();
124 | };
125 |
126 | #endif // HOME_H
127 |
--------------------------------------------------------------------------------
/Client/icon/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Client/icon/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/1.png
--------------------------------------------------------------------------------
/Client/icon/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/10.png
--------------------------------------------------------------------------------
/Client/icon/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/11.png
--------------------------------------------------------------------------------
/Client/icon/1112.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/1112.png
--------------------------------------------------------------------------------
/Client/icon/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/12.png
--------------------------------------------------------------------------------
/Client/icon/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/13.png
--------------------------------------------------------------------------------
/Client/icon/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/14.png
--------------------------------------------------------------------------------
/Client/icon/145.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/145.png
--------------------------------------------------------------------------------
/Client/icon/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/15.png
--------------------------------------------------------------------------------
/Client/icon/155.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/155.png
--------------------------------------------------------------------------------
/Client/icon/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/16.png
--------------------------------------------------------------------------------
/Client/icon/17.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/17.png
--------------------------------------------------------------------------------
/Client/icon/18.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/18.png
--------------------------------------------------------------------------------
/Client/icon/19.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/19.png
--------------------------------------------------------------------------------
/Client/icon/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/2.png
--------------------------------------------------------------------------------
/Client/icon/20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/20.png
--------------------------------------------------------------------------------
/Client/icon/21.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/21.png
--------------------------------------------------------------------------------
/Client/icon/22.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/22.png
--------------------------------------------------------------------------------
/Client/icon/23.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/23.png
--------------------------------------------------------------------------------
/Client/icon/24.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/24.png
--------------------------------------------------------------------------------
/Client/icon/25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/25.png
--------------------------------------------------------------------------------
/Client/icon/26.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/26.png
--------------------------------------------------------------------------------
/Client/icon/27.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/27.png
--------------------------------------------------------------------------------
/Client/icon/28.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/28.png
--------------------------------------------------------------------------------
/Client/icon/29.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/29.png
--------------------------------------------------------------------------------
/Client/icon/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/3.png
--------------------------------------------------------------------------------
/Client/icon/30.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/30.png
--------------------------------------------------------------------------------
/Client/icon/31.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/31.png
--------------------------------------------------------------------------------
/Client/icon/32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/32.png
--------------------------------------------------------------------------------
/Client/icon/33.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/33.png
--------------------------------------------------------------------------------
/Client/icon/34.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/34.png
--------------------------------------------------------------------------------
/Client/icon/35.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/35.png
--------------------------------------------------------------------------------
/Client/icon/36.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/36.png
--------------------------------------------------------------------------------
/Client/icon/37.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/37.png
--------------------------------------------------------------------------------
/Client/icon/38.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/38.png
--------------------------------------------------------------------------------
/Client/icon/39.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/39.png
--------------------------------------------------------------------------------
/Client/icon/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/4.png
--------------------------------------------------------------------------------
/Client/icon/40.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/40.png
--------------------------------------------------------------------------------
/Client/icon/41.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/41.png
--------------------------------------------------------------------------------
/Client/icon/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/5.png
--------------------------------------------------------------------------------
/Client/icon/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/6.png
--------------------------------------------------------------------------------
/Client/icon/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/7.png
--------------------------------------------------------------------------------
/Client/icon/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/8.png
--------------------------------------------------------------------------------
/Client/icon/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/9.png
--------------------------------------------------------------------------------
/Client/icon/R-C.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Client/icon/R-C.png
--------------------------------------------------------------------------------
/Client/isfriend.cpp:
--------------------------------------------------------------------------------
1 | #include "isfriend.h"
2 | #include "ui_isfriend.h"
3 | #include
4 |
5 | IsFriend::IsFriend(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::IsFriend)
8 | {
9 | ui->setupUi(this);
10 | }
11 |
12 | IsFriend::~IsFriend()
13 | {
14 | delete ui;
15 | }
16 |
17 |
18 | void IsFriend::init(int id, QString name, QPixmap pix)
19 | {
20 | this->name=name;
21 | this->id=id;
22 | QPixmap pixmap = pix.scaled(ui->label->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); // 缩放图片以适应标签大小
23 | QPixmap circularPixmap(ui->label->size());
24 | circularPixmap.fill(Qt::transparent);
25 | QPainter painter(&circularPixmap);
26 | painter.setRenderHint(QPainter::Antialiasing);
27 | painter.setBrush(QBrush(pixmap));
28 | painter.setPen(Qt::NoPen);
29 | painter.drawEllipse(0, 0, ui->label->width(), ui->label->height());
30 | ui->label->setPixmap(circularPixmap);
31 | ui->label->setAlignment(Qt::AlignCenter);
32 | ui->label_2->setText(name);
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/Client/isfriend.h:
--------------------------------------------------------------------------------
1 | #ifndef ISFRIEND_H
2 | #define ISFRIEND_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class IsFriend;
8 | }
9 |
10 | class IsFriend : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit IsFriend(QWidget *parent = nullptr);
16 | ~IsFriend();
17 | private:
18 | int id;
19 | QString name;
20 |
21 | public:
22 | void init(int id,QString name,QPixmap pix);
23 |
24 | private:
25 | Ui::IsFriend *ui;
26 | };
27 |
28 | #endif // ISFRIEND_H
29 |
--------------------------------------------------------------------------------
/Client/isfriend.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | IsFriend
4 |
5 |
6 |
7 | 0
8 | 0
9 | 400
10 | 80
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
21 | 50
22 | 50
23 |
24 |
25 |
26 |
27 | 50
28 | 50
29 |
30 |
31 |
32 | background-color: rgba(255, 255, 255, 0);
33 |
34 |
35 |
36 |
37 |
38 |
39 | -
40 |
41 |
42 | color: rgb(0, 0, 0);
43 | background-color: rgba(255, 255, 255, 0);
44 |
45 |
46 |
47 |
48 |
49 |
50 | -
51 |
52 |
53 | color: rgb(0, 0, 0);
54 | font: 10pt "Microsoft YaHei UI";
55 | background-color: rgba(255, 255, 255, 0);
56 |
57 |
58 | 已成为好友
59 |
60 |
61 | Qt::AlignCenter
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/Client/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 |
3 | #include
4 | #include"form.h"
5 |
6 | int main(int argc, char *argv[])
7 | {
8 | QApplication a(argc, argv);
9 | Form form;
10 | form.show();
11 | // MainWindow w;
12 | // w.show();
13 | return a.exec();
14 | }
15 |
--------------------------------------------------------------------------------
/Client/mainwindow.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 | #include "ui_mainwindow.h"
3 |
4 | MainWindow::MainWindow(QWidget *parent)
5 | : QMainWindow(parent)
6 | , ui(new Ui::MainWindow)
7 | {
8 | ui->setupUi(this);
9 | }
10 |
11 | MainWindow::~MainWindow()
12 | {
13 | delete ui;
14 | }
15 |
--------------------------------------------------------------------------------
/Client/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 |
6 | QT_BEGIN_NAMESPACE
7 | namespace Ui {
8 | class MainWindow;
9 | }
10 | QT_END_NAMESPACE
11 |
12 | class MainWindow : public QMainWindow
13 | {
14 | Q_OBJECT
15 |
16 | public:
17 | MainWindow(QWidget *parent = nullptr);
18 | ~MainWindow();
19 |
20 | private:
21 | Ui::MainWindow *ui;
22 | };
23 | #endif // MAINWINDOW_H
24 |
--------------------------------------------------------------------------------
/Client/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 800
10 | 600
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Client/myinformation.cpp:
--------------------------------------------------------------------------------
1 | #include "myinformation.h"
2 | #include "ui_myinformation.h"
3 | #include
4 | #include
5 |
6 | Myinformation::Myinformation(QWidget *parent)
7 | : QWidget(parent)
8 | , ui(new Ui::Myinformation)
9 | {
10 | ui->setupUi(this);
11 | setWindowFlags(Qt::FramelessWindowHint);
12 | setAttribute(Qt::WA_TranslucentBackground);
13 | }
14 |
15 | Myinformation::~Myinformation()
16 | {
17 | delete ui;
18 | }
19 |
20 | void Myinformation::init(int id, QString name, QPixmap pix)
21 | {
22 | ui->label_5->setText(QString::number(id));
23 | ui->lineEdit_2->setText(name);
24 | QPixmap pixmap = pix.scaled(ui->pushButton->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
25 | QPixmap circularPixmap(ui->pushButton->size());
26 | circularPixmap.fill(Qt::transparent);
27 | QPainter painter(&circularPixmap);
28 | painter.setRenderHint(QPainter::Antialiasing);
29 | painter.setBrush(QBrush(pixmap));
30 | painter.setPen(Qt::NoPen);
31 | painter.drawEllipse(0, 0, ui->pushButton->width(), ui->pushButton->height());
32 | ui->pushButton->setIcon(circularPixmap);
33 | }
34 |
35 | void Myinformation::paintEvent(QPaintEvent *event)
36 | {
37 | QPainter painter(this);
38 | painter.setRenderHint(QPainter::Antialiasing);
39 | painter.setBrush(Qt::white);
40 | painter.setPen(Qt::NoPen);
41 | painter.drawRoundedRect(rect(), 15, 15);
42 | }
43 |
44 | void Myinformation::on_pushButton_2_clicked()
45 | {
46 | emit confirmclicked(ui->lineEdit_2->text());
47 | }
48 |
49 |
50 | void Myinformation::on_pushButton_3_clicked()
51 | {
52 | emit cancelclicked();
53 | }
54 |
55 |
56 | void Myinformation::on_pushButton_clicked()
57 | {
58 | QString path=QFileDialog::getOpenFileName(this,"选择头像","",tr("PNG图片 (*.png)"));
59 | if(path.isEmpty()){
60 | return;
61 | }
62 | QByteArray newicon;
63 | QFile file(path);
64 | file.open(QIODevice::ReadOnly);
65 | const qint64 CHUNK_SIZE = 1024 * 50;
66 | while(!file.atEnd()){
67 | QByteArray chunk=file.read(CHUNK_SIZE);
68 | newicon.append(chunk);
69 | }
70 | file.close();
71 | QPixmap pix;
72 | pix.loadFromData(newicon);
73 | emit iconchanged(path);
74 | emit tohomeicon(newicon);
75 | init(ui->label_5->text().toInt(),ui->lineEdit_2->text(),pix);
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/Client/myinformation.h:
--------------------------------------------------------------------------------
1 | #ifndef MYINFORMATION_H
2 | #define MYINFORMATION_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class Myinformation;
8 | }
9 |
10 | class Myinformation : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit Myinformation(QWidget *parent = nullptr);
16 | ~Myinformation();
17 |
18 | public:
19 |
20 | void init(int id,QString name,QPixmap pix);
21 |
22 | protected:
23 | void paintEvent(QPaintEvent* event)override;
24 |
25 | private:
26 | Ui::Myinformation *ui;
27 | public:signals:
28 | void confirmclicked(QString name);
29 | void cancelclicked();
30 | void iconchanged(QString path);
31 | void tohomeicon(QByteArray data);
32 | private slots:
33 | void on_pushButton_2_clicked();
34 | void on_pushButton_3_clicked();
35 | void on_pushButton_clicked();
36 | };
37 |
38 | #endif // MYINFORMATION_H
39 |
--------------------------------------------------------------------------------
/Client/myinformation.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Myinformation
4 |
5 |
6 |
7 | 0
8 | 0
9 | 355
10 | 324
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 |
-
21 |
22 |
23 | Qt::Horizontal
24 |
25 |
26 |
27 | 135
28 | 20
29 |
30 |
31 |
32 |
33 | -
34 |
35 |
36 | font: 12pt "Microsoft YaHei UI";
37 | color: rgb(0, 0, 0);
38 |
39 |
40 | 编辑资料
41 |
42 |
43 | Qt::AlignCenter
44 |
45 |
46 |
47 | -
48 |
49 |
50 | Qt::Horizontal
51 |
52 |
53 |
54 | 134
55 | 20
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 | -
64 |
65 |
66 |
-
67 |
68 |
69 | color: rgb(0, 0, 0);
70 | font: 12pt "Microsoft YaHei UI";
71 |
72 |
73 | 头像
74 |
75 |
76 |
77 | -
78 |
79 |
80 | Qt::Horizontal
81 |
82 |
83 |
84 | 226
85 | 20
86 |
87 |
88 |
89 |
90 | -
91 |
92 |
93 |
94 | 70
95 | 70
96 |
97 |
98 |
99 |
100 | 70
101 | 70
102 |
103 |
104 |
105 | background-color: rgba(255, 255, 255, 0);
106 |
107 |
108 |
109 |
110 |
111 |
112 | 68
113 | 68
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 | -
122 |
123 |
124 |
-
125 |
126 |
127 | color: rgb(0, 0, 0);
128 | font: 12pt "Microsoft YaHei UI";
129 |
130 |
131 | id
132 |
133 |
134 |
135 | -
136 |
137 |
138 | Qt::Horizontal
139 |
140 |
141 |
142 | 259
143 | 20
144 |
145 |
146 |
147 |
148 | -
149 |
150 |
151 | background-color: rgba(255, 255, 255, 0);
152 | color: rgb(0, 0, 0);
153 | font: 12pt "Microsoft YaHei UI";
154 |
155 |
156 | 1111111
157 |
158 |
159 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter
160 |
161 |
162 |
163 |
164 |
165 |
166 | -
167 |
168 |
169 |
-
170 |
171 |
172 | color: rgb(0, 0, 0);
173 | font: 12pt "Microsoft YaHei UI";
174 |
175 |
176 | 昵称
177 |
178 |
179 |
180 | -
181 |
182 |
183 | Qt::Horizontal
184 |
185 |
186 |
187 | 143
188 | 20
189 |
190 |
191 |
192 |
193 | -
194 |
195 |
196 |
197 | 160
198 | 0
199 |
200 |
201 |
202 |
203 | 160
204 | 16777215
205 |
206 |
207 |
208 | background-color: rgba(255, 255, 255, 0);
209 | color: rgb(0, 0, 0);
210 | border: none;
211 | font: 12pt "Microsoft YaHei UI";
212 |
213 |
214 | 111
215 |
216 |
217 | Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter
218 |
219 |
220 |
221 |
222 |
223 |
224 | -
225 |
226 |
227 |
-
228 |
229 |
230 |
231 |
232 |
233 | 确定
234 |
235 |
236 |
237 | -
238 |
239 |
240 | Qt::Horizontal
241 |
242 |
243 |
244 | 160
245 | 20
246 |
247 |
248 |
249 |
250 | -
251 |
252 |
253 | 取消
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
--------------------------------------------------------------------------------
/Client/mysendfile.cpp:
--------------------------------------------------------------------------------
1 | #include "mysendfile.h"
2 | #include "ui_mysendfile.h"
3 | #include
4 |
5 | MySendfile::MySendfile(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::MySendfile)
8 | {
9 | ui->setupUi(this);
10 | }
11 |
12 | MySendfile::~MySendfile()
13 | {
14 | delete ui;
15 | }
16 |
17 |
18 | void MySendfile::setfileicon(QString filepath)
19 | {
20 | QFileInfo fileinfo(filepath);
21 | ui->label_2->setText(fileinfo.fileName());
22 | qint64 size=fileinfo.size();
23 | double sizeKB=size/1024.0;
24 | if(sizeKB>=1024){
25 | double sizeMB=sizeKB/1024.0;
26 | if(sizeMB>=1024){
27 | double sizeGB=sizeMB/1024.0;
28 | ui->label_3->setText(QString::number(sizeGB)+"GB");
29 | }else{
30 | ui->label_3->setText(QString::number(sizeMB)+"MB");
31 | }
32 | }else{
33 | ui->label_3->setText(QString::number(sizeKB)+"KB");
34 | }
35 | QString suffix = fileinfo.suffix().toLower();
36 | QPixmap pix;
37 | if(suffix=="bmp"){
38 | pix=QPixmap(":/icon/20.png");
39 | }else if(suffix=="dos"){
40 | pix=QPixmap(":/icon/21.png");
41 | }else if(suffix=="eps"){
42 | pix=QPixmap(":/icon/22.png");
43 | }else if(suffix=="exe"){
44 | pix=QPixmap(":/icon/23.png");
45 | }else if(suffix=="html"){
46 | pix=QPixmap(":/icon/24.png");
47 | }else if(suffix=="mp4"){
48 | pix=QPixmap(":/icon/25.png");
49 | }else if(suffix=="pdf"){
50 | pix=QPixmap(":/icon/26.png");
51 | }else if(suffix=="ppt"){
52 | pix=QPixmap(":/icon/27.png");
53 | }else if(suffix=="psd"){
54 | pix=QPixmap(":/icon/28.png");
55 | }else if(suffix=="txt"){
56 | pix=QPixmap(":/icon/29.png");
57 | }else if(suffix=="xls"){
58 | pix=QPixmap(":/icon/30.png");
59 | }else if(suffix=="xml"){
60 | pix=QPixmap(":/icon/31.png");
61 | }else if(suffix=="zip"){
62 | pix=QPixmap(":/icon/34.png");
63 | }else if(suffix==""){
64 | pix=QPixmap(":/icon/33.png");
65 | }else{
66 | pix=QPixmap(":/icon/32.png");
67 | }
68 | ui->label->setText("");
69 | ui->label->setPixmap(pix);
70 | ui->label_4->setText("发送中");
71 | }
72 |
73 | void MySendfile::setvalue(int value)
74 | {
75 | ui->progressBar->setValue(value);
76 | if(value==100){
77 | ui->label_4->setText("发送完毕");
78 | }
79 | }
80 |
81 |
--------------------------------------------------------------------------------
/Client/mysendfile.h:
--------------------------------------------------------------------------------
1 | #ifndef MYSENDFILE_H
2 | #define MYSENDFILE_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class MySendfile;
8 | }
9 |
10 | class MySendfile : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit MySendfile(QWidget *parent = nullptr);
16 | ~MySendfile();
17 |
18 | void setfileicon(QString filepath);
19 | void setvalue(int value);
20 |
21 | private:
22 | Ui::MySendfile *ui;
23 | };
24 |
25 | #endif // MYSENDFILE_H
26 |
--------------------------------------------------------------------------------
/Client/mysendfile.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MySendfile
4 |
5 |
6 |
7 | 0
8 | 0
9 | 300
10 | 100
11 |
12 |
13 |
14 |
15 | 300
16 | 0
17 |
18 |
19 |
20 |
21 | 300
22 | 16777215
23 |
24 |
25 |
26 | Form
27 |
28 |
29 |
30 | 0
31 |
32 |
33 | 0
34 |
35 |
36 | 0
37 |
38 |
39 | 0
40 |
41 |
42 | 0
43 |
44 | -
45 |
46 |
47 |
48 | 300
49 | 0
50 |
51 |
52 |
53 |
54 | 300
55 | 16777215
56 |
57 |
58 |
59 | border-radius:10px;
60 | background-color: #0099ff;
61 |
62 |
63 |
-
64 |
65 |
-
66 |
67 |
68 |
69 | 50
70 | 60
71 |
72 |
73 |
74 |
75 | 50
76 | 60
77 |
78 |
79 |
80 | TextLabel
81 |
82 |
83 | true
84 |
85 |
86 |
87 | -
88 |
89 |
-
90 |
91 |
92 | TextLabel
93 |
94 |
95 |
96 | -
97 |
98 |
-
99 |
100 |
101 | TextLabel
102 |
103 |
104 |
105 | -
106 |
107 |
108 | Qt::Horizontal
109 |
110 |
111 |
112 | 40
113 | 20
114 |
115 |
116 |
117 |
118 | -
119 |
120 |
121 | TextLabel
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 | -
132 |
133 |
134 |
135 | 16777215
136 | 3
137 |
138 |
139 |
140 | 0
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
--------------------------------------------------------------------------------
/Client/prompt.cpp:
--------------------------------------------------------------------------------
1 | #include "prompt.h"
2 | #include "ui_prompt.h"
3 |
4 | Prompt::Prompt(QWidget *parent)
5 | : QWidget(parent)
6 | , ui(new Ui::Prompt)
7 | {
8 | ui->setupUi(this);
9 | setWindowFlags(Qt::FramelessWindowHint);
10 | setAttribute(Qt::WA_TranslucentBackground);
11 | setStyleSheet(
12 | "QWidget {"
13 | " background-color: white;"
14 | " border-radius: 15px;"
15 | " border: 2px solid #CCCCCC;"
16 | "}"
17 | );
18 | }
19 |
20 | Prompt::~Prompt()
21 | {
22 | delete ui;
23 | }
24 |
--------------------------------------------------------------------------------
/Client/prompt.h:
--------------------------------------------------------------------------------
1 | #ifndef PROMPT_H
2 | #define PROMPT_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class Prompt;
8 | }
9 |
10 | class Prompt : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit Prompt(QWidget *parent = nullptr);
16 | ~Prompt();
17 |
18 | private:
19 | Ui::Prompt *ui;
20 | };
21 |
22 | #endif // PROMPT_H
23 |
--------------------------------------------------------------------------------
/Client/prompt.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Prompt
4 |
5 |
6 |
7 | 0
8 | 0
9 | 240
10 | 80
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 | color: rgb(0, 0, 0);
21 | font: 10pt "Microsoft YaHei UI";
22 |
23 |
24 | 有添加好友申请
25 |
26 |
27 | Qt::AlignCenter
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Client/register.cpp:
--------------------------------------------------------------------------------
1 | #include "register.h"
2 | #include "ui_register.h"
3 | #include
4 | #include
5 | #include
6 |
7 | Register::Register(QWidget *parent)
8 | : QWidget(parent)
9 | , ui(new Ui::Register)
10 | {
11 | ui->setupUi(this);
12 | setWindowFlags(Qt::FramelessWindowHint);
13 | setAttribute(Qt::WA_TranslucentBackground);
14 | this->setWindowOpacity(1.0);
15 | ui->pushButton_3->setStyleSheet(
16 | "QPushButton{"
17 | "border-image: url(:/icon/1.png);}"
18 | "QPushButton:hover {"
19 | "border-image: url(:/icon/2.png);"
20 | "}"
21 | );
22 | ui->pushButton->setStyleSheet(
23 | "QPushButton{"
24 | "background-color: rgb(85, 170, 255);"
25 | "font: 14pt \"Microsoft YaHei UI\";"
26 | "color: rgb(255, 255, 255);"
27 | "border-radius:6px;}"
28 | "QPushButton:hover {"
29 | "background-color: rgb(50, 131, 231);"
30 | "font: 14pt \"Microsoft YaHei UI\";"
31 | "color: rgb(255, 255, 255);"
32 | "border-radius:6px;"
33 | "}"
34 | );
35 | }
36 |
37 | Register::~Register()
38 | {
39 | delete ui;
40 | }
41 |
42 | void Register::paintEvent(QPaintEvent *event)
43 | {
44 | QPainter painter(this);
45 | painter.setRenderHint(QPainter::Antialiasing);
46 | painter.setBrush(QColor(240,240,240));
47 | painter.setPen(Qt::NoPen);
48 | painter.drawRoundedRect(rect(), 15, 15);
49 | }
50 |
51 | void Register::on_pushButton_clicked()//点击注册
52 | {
53 | QString name=ui->lineEdit->text();
54 | QString password=ui->lineEdit_2->text();
55 | if(name.size()>10||password.size()>20){
56 | QMessageBox::warning(this,"注意","用户名不要超过十位,密码不要超过二十位!!!");
57 | return;
58 | }
59 | if(name.size()==0||password.size()<3){
60 | QMessageBox::warning(this,"注意","用户名不能为空,密码不能少于三位!!!");
61 | return;
62 | }
63 | emit login(name,password);
64 | }
65 |
66 |
67 | void Register::on_pushButton_2_clicked()//点击返回登录
68 | {
69 | emit closewidget();
70 | }
71 |
72 |
73 | void Register::on_pushButton_3_clicked()
74 | {
75 | this->close();
76 | }
77 |
78 |
--------------------------------------------------------------------------------
/Client/register.h:
--------------------------------------------------------------------------------
1 | #ifndef REGISTER_H
2 | #define REGISTER_H
3 |
4 | #include
5 |
6 |
7 | namespace Ui {
8 | class Register;
9 | }
10 |
11 | class Register : public QWidget
12 | {
13 | Q_OBJECT
14 |
15 | public:
16 | explicit Register(QWidget *parent = nullptr);
17 | ~Register();
18 |
19 | private:
20 |
21 | protected:
22 | void paintEvent(QPaintEvent *event)override;
23 |
24 | private slots:
25 | void on_pushButton_clicked();
26 |
27 | void on_pushButton_2_clicked();
28 |
29 | void on_pushButton_3_clicked();
30 |
31 | public:signals:
32 | void closewidget();
33 | void login(QString name,QString password);
34 |
35 | private:
36 | Ui::Register *ui;
37 | };
38 |
39 | #endif // REGISTER_H
40 |
--------------------------------------------------------------------------------
/Client/register.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Register
4 |
5 |
6 |
7 | 0
8 | 0
9 | 325
10 | 438
11 |
12 |
13 |
14 | Form
15 |
16 |
17 |
18 | 0
19 |
20 |
21 | 0
22 |
23 |
24 | 0
25 |
26 |
27 | 0
28 |
29 |
30 | 0
31 |
32 | -
33 |
34 |
35 | 15
36 |
37 |
-
38 |
39 |
40 |
-
41 |
42 |
43 | Qt::Horizontal
44 |
45 |
46 |
47 | 289
48 | 20
49 |
50 |
51 |
52 |
53 | -
54 |
55 |
56 |
57 | 15
58 | 15
59 |
60 |
61 |
62 |
63 | 15
64 | 15
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | -
76 |
77 |
78 | Qt::Vertical
79 |
80 |
81 |
82 | 308
83 | 18
84 |
85 |
86 |
87 |
88 | -
89 |
90 |
-
91 |
92 |
93 | Qt::Horizontal
94 |
95 |
96 |
97 | 40
98 | 20
99 |
100 |
101 |
102 |
103 | -
104 |
105 |
106 |
107 | 70
108 | 70
109 |
110 |
111 |
112 |
113 | 1000
114 | 70
115 |
116 |
117 |
118 | background-color: rgba(255, 255, 255, 0);
119 | font: 18pt "Microsoft YaHei UI";
120 | color: rgb(0, 0, 0);
121 |
122 |
123 | 欢迎注册
124 |
125 |
126 | Qt::AlignCenter
127 |
128 |
129 |
130 | -
131 |
132 |
133 | Qt::Horizontal
134 |
135 |
136 |
137 | 40
138 | 20
139 |
140 |
141 |
142 |
143 |
144 |
145 | -
146 |
147 |
-
148 |
149 |
150 | Qt::Horizontal
151 |
152 |
153 |
154 | 40
155 | 20
156 |
157 |
158 |
159 |
160 | -
161 |
162 |
163 |
164 | 250
165 | 40
166 |
167 |
168 |
169 | border-radius:8px;
170 | background-color: rgb(255, 255, 255);
171 | font: 16pt "Microsoft YaHei UI";
172 |
173 |
174 | Qt::AlignCenter
175 |
176 |
177 | 输入昵称
178 |
179 |
180 |
181 | -
182 |
183 |
184 | Qt::Horizontal
185 |
186 |
187 |
188 | 40
189 | 20
190 |
191 |
192 |
193 |
194 |
195 |
196 | -
197 |
198 |
-
199 |
200 |
201 | Qt::Horizontal
202 |
203 |
204 |
205 | 40
206 | 20
207 |
208 |
209 |
210 |
211 | -
212 |
213 |
214 |
215 | 250
216 | 40
217 |
218 |
219 |
220 | border-radius:8px;
221 | background-color: rgb(255, 255, 255);
222 | font: 16pt "Microsoft YaHei UI";
223 |
224 |
225 |
226 |
227 |
228 | QLineEdit::Password
229 |
230 |
231 | Qt::AlignCenter
232 |
233 |
234 | 输入密码
235 |
236 |
237 |
238 | -
239 |
240 |
241 | Qt::Horizontal
242 |
243 |
244 |
245 | 40
246 | 20
247 |
248 |
249 |
250 |
251 |
252 |
253 | -
254 |
255 |
256 | Qt::Vertical
257 |
258 |
259 |
260 | 308
261 | 13
262 |
263 |
264 |
265 |
266 | -
267 |
268 |
-
269 |
270 |
271 | Qt::Horizontal
272 |
273 |
274 |
275 | 40
276 | 20
277 |
278 |
279 |
280 |
281 | -
282 |
283 |
284 |
285 | 250
286 | 30
287 |
288 |
289 |
290 |
291 | 200
292 | 30
293 |
294 |
295 |
296 | background-color: rgb(85, 170, 255);
297 | font: 14pt "Microsoft YaHei UI";
298 | color: rgb(255, 255, 255);
299 | border-radius:6px;
300 |
301 |
302 | 注册
303 |
304 |
305 |
306 | -
307 |
308 |
309 | Qt::Horizontal
310 |
311 |
312 |
313 | 40
314 | 20
315 |
316 |
317 |
318 |
319 |
320 |
321 | -
322 |
323 |
-
324 |
325 |
326 | Qt::Horizontal
327 |
328 |
329 |
330 | 40
331 | 20
332 |
333 |
334 |
335 |
336 | -
337 |
338 |
339 | background-color: rgba(255, 255, 255, 0);
340 |
341 |
342 | 返回登录
343 |
344 |
345 |
346 | -
347 |
348 |
349 | Qt::Horizontal
350 |
351 |
352 |
353 | 40
354 | 20
355 |
356 |
357 |
358 |
359 |
360 |
361 | -
362 |
363 |
364 | Qt::Vertical
365 |
366 |
367 |
368 | 20
369 | 85
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
--------------------------------------------------------------------------------
/Client/senddata.cpp:
--------------------------------------------------------------------------------
1 | #include "senddata.h"
2 | #include
3 | #include
4 | #include
5 |
6 | SendData::SendData(QObject *parent)
7 | : QObject{parent}
8 | {}
9 |
10 |
11 | void SendData::init()
12 | {
13 | mark=QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");//生成客户端唯一标识
14 | udpsocket=new QUdpSocket(this);
15 | udpsocket->bind(QHostAddress::AnyIPv4, 45454,
16 | QUdpSocket::ReuseAddressHint | QUdpSocket::ShareAddress);
17 | connect(udpsocket,&QUdpSocket::readyRead,this,&SendData::getudpsocket);
18 | }
19 |
20 | void SendData::on_msgreadyRead()//处理msgsocket接收的消息
21 | {
22 |
23 | QMutexLocker locker(&msgmutex);
24 | while(msgsocket->bytesAvailable()>0){
25 | if (msgsocket->bytesAvailable() < sizeof(qint64)){
26 | return;
27 | }
28 | qint64 len;
29 | msgsocket->read(reinterpret_cast(&len), sizeof(len));
30 | len = qFromBigEndian(len);
31 | while (msgsocket->bytesAvailable() < len) {
32 | if (!msgsocket->waitForReadyRead(1000)) break;
33 | }
34 | QByteArray data = msgsocket->read(len);
35 | msgqueue.enqueue(data);
36 | }
37 | QMetaObject::invokeMethod(this, "handleMessages", Qt::QueuedConnection);//异步处理
38 | }
39 |
40 | void SendData::on_filereadyRead()//处理filesocket接收的消息
41 | {
42 | //QMutexLocker locker(&filemutex);
43 | while(filesocket->bytesAvailable()>0){
44 | if(filesocket->bytesAvailable()<(sizeof(qint64)*2)){
45 | return;
46 | }
47 | qint64 fileid;
48 | filesocket->read(reinterpret_cast(&fileid), sizeof(fileid));
49 | fileid = qFromBigEndian(fileid);
50 | qint64 len;
51 | filesocket->read(reinterpret_cast(&len), sizeof(len));
52 | len = qFromBigEndian(len);
53 | while (filesocket->bytesAvailable() < len) {
54 | if (!filesocket->waitForReadyRead(1000)) break;
55 | }
56 |
57 | QByteArray data = filesocket->read(len);
58 | FileTransferContext *ctx=NULL;
59 | QMutexLocker locker(&filemutex);
60 | for(auto &task:filequeue){
61 | if(fileid==task.fileId){
62 | ctx=&task;
63 | break;
64 | }
65 | }
66 | if(ctx==NULL){//如果没有找到任务,存入临时队列
67 | qDebug()<<"没有找到任务"<<'\n';
68 | beforfilemutex.lock();
69 | BeforFiledata filedata;
70 | filedata.fileid=fileid;
71 | filedata.data.append(data);
72 | beforfilequeue.enqueue(filedata);
73 | beforfilemutex.unlock();
74 | }else{
75 | if(ctx->type=="sendfile"){
76 | QFile file(ctx->path);
77 | file.open(QIODevice::WriteOnly|QIODevice::Append);
78 | ctx->receivedSize+=data.size();
79 | file.write(data);
80 | file.close();
81 | }else{
82 | ctx->receivedSize+=data.size();
83 | ctx->data.append(data);
84 | }
85 | qDebug()<receivedSize<<"/"<totalSize<<'\n';
86 | if(ctx->totalSize>0&&ctx->receivedSize>=ctx->totalSize){
87 | if(ctx->type=="logon"){
88 | emit logonsuccseeicon(ctx->data);
89 | }else if(ctx->type=="login"){
90 | emit loginsuccseeicon(ctx->data);
91 | }else if(ctx->type=="initfriend"||ctx->type=="agreeadd"){
92 | emit initfriend(ctx->friendid,ctx->friendname,ctx->data);
93 | }else if(ctx->type=="selectuser"){
94 | emit showselectuser(ctx->friendid,ctx->friendname,ctx->data);
95 | }else if(ctx->type=="friendrequest"){
96 | emit showfriendrequest(ctx->friendid,ctx->friendname,ctx->data);
97 | }else if(ctx->type=="sendfile"){
98 | emit friendsendfilesize((int)(ctx->receivedSize*100/ctx->totalSize));
99 | }else if(ctx->type=="sendpicture"){
100 | emit friendsendpicture(ctx->friendid,ctx->data,ctx->h,ctx->w);
101 | }else if(ctx->type=="friendpicturechanged"){
102 | qDebug()<<"更换头像"<<'\n';
103 | emit friendpicturechanged(ctx->friendid,ctx->data);
104 | }
105 | filequeue.removeAll(*ctx);
106 | }
107 | }
108 | }
109 |
110 | }
111 |
112 |
113 |
114 | void SendData::getudpsocket()
115 | {
116 | while (udpsocket->hasPendingDatagrams()) {
117 | QByteArray datagram;
118 | datagram.resize(udpsocket->pendingDatagramSize());
119 | QHostAddress sender;
120 | quint16 port;
121 | udpsocket->readDatagram(datagram.data(), datagram.size(), &sender, &port);
122 |
123 | QString msg = QString::fromUtf8(datagram);
124 | if (msg.startsWith("SERVER:")) {
125 | QStringList parts = msg.split(':');
126 | if (parts.size() == 3) {
127 | QString serverIP = parts[1];
128 | int serverPort = parts[2].toInt();
129 | connectToServer(serverIP, serverPort);
130 | }
131 | }
132 | }
133 | }
134 |
135 |
136 |
137 | void SendData::connectToServer(QString ip, int port)
138 | {
139 | if (!msgsocket&&!filesocket){
140 | msgsocket=new QTcpSocket(this);
141 | connect(msgsocket,&QTcpSocket::disconnected,this,[=](){
142 | msgsocket->deleteLater();
143 | msgsocket=NULL;
144 | emit deletesocket();
145 | });
146 | connect(msgsocket,&QTcpSocket::readyRead,this,&SendData::on_msgreadyRead);
147 | filesocket=new QTcpSocket(this);
148 | connect(filesocket,&QTcpSocket::disconnected,this,[=](){
149 | filesocket->deleteLater();
150 | filesocket=NULL;
151 | emit deletesocket();
152 | });
153 | connect(filesocket,&QTcpSocket::readyRead,this,&SendData::on_filereadyRead);
154 | }
155 | if (msgsocket->state() == QAbstractSocket::ConnectedState) {
156 | return;
157 | }
158 | msgsocket->connectToHost(ip, port);
159 | filesocket->connectToHost(ip,port+1);
160 | //客户端发送信号,让服务器对应的两个socket绑定
161 | QJsonObject json;
162 | json["type"]="connect";
163 | json["mark"]=mark;
164 | QJsonDocument doc(json);
165 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
166 | packingjson(m_data);
167 |
168 |
169 | QJsonObject filejson;
170 | filejson["mark"]=mark;
171 | QJsonDocument filedoc(filejson);
172 | QByteArray filedata = filedoc.toJson(QJsonDocument::Compact);
173 | QByteArray newfiledata;
174 | qint64 len = qToBigEndian(filedata.size());
175 | qint64 filemark = qToBigEndian(0);
176 | newfiledata.append(reinterpret_cast(&filemark), sizeof(filemark));
177 | newfiledata.append(reinterpret_cast(&len), sizeof(len));
178 | newfiledata.append(filedata);
179 | filesocket->write(newfiledata);
180 | }
181 |
182 | void SendData::packingjson(const QByteArray &m_data)//把要发送的json打包成自定义的格式
183 | {
184 | qint64 len = qToBigEndian(m_data.size());//大端通信
185 | QByteArray data;
186 | data.append(reinterpret_cast(&len), sizeof(len));
187 | data.append(m_data);
188 | msgsocket->write(data);
189 | }
190 |
191 | void SendData::handleMessages()//异步处理msgqueue中的数据
192 | {
193 | //QMutexLocker locker(&msgmutex);
194 | while(!msgqueue.empty()){
195 | QByteArray data = msgqueue.front();
196 | msgqueue.pop_front();
197 | QJsonDocument doc = QJsonDocument::fromJson(data);
198 | processJson(doc.object());
199 | }
200 | }
201 |
202 | void SendData::processJson(const QJsonObject &json)
203 | {
204 | QString type=json["type"].toString();
205 | qDebug()<<"json到达 type:"<(json["size"].toDouble());
212 | qint64 fileid=static_cast(json["fileid"].toDouble());
213 | addfilequeue(fileid,type,size);
214 | emit logonsuccsee(id,name);
215 | qDebug()<<"登录成功"<<'\n';
216 | }else if(num==0){
217 | emit logonfailure();
218 | qDebug()<<"登录失败"<<'\n';
219 | }else if(num==2){
220 | emit logoning();
221 | qDebug()<<"其他设备登录"<<'\n';
222 | }
223 | }else if(type=="login"){//注册
224 | int num=json["num"].toInt();
225 | if(num==1){
226 | qDebug()<<"收到注册返回"<<'\n';
227 | id=json["id"].toInt();
228 | name=json["name"].toString();
229 | qint64 size=static_cast(json["size"].toDouble());
230 | qint64 fileid=static_cast(json["fileid"].toDouble());
231 | addfilequeue(fileid,type,size);
232 | emit loginsuccsee(id,name);
233 | }else{
234 | emit loginfailure();
235 | }
236 | }else if(type=="initfriend"||type=="agreeadd"){
237 | int friendid=json["friendid"].toInt();
238 | QString friendname=json["friendname"].toString();
239 | qint64 size=static_cast(json["size"].toDouble());
240 | qint64 fileid=static_cast(json["fileid"].toDouble());
241 | addfilequeue(fileid,type,size,friendid,friendname);
242 | }else if(type=="selectuser"){
243 | int num=json["num"].toInt();
244 | if(num){
245 | int friendid=json["friendid"].toInt();
246 | QString friendname=json["friendname"].toString();
247 | qint64 size=static_cast(json["size"].toDouble());
248 | qint64 fileid=static_cast(json["fileid"].toDouble());
249 | addfilequeue(fileid,type,size,friendid,friendname);
250 | }else{
251 | emit isnullid();
252 | }
253 | }else if(type=="friendrequest"){
254 | int friendid=json["friendid"].toInt();
255 | QString friendname=json["friendname"].toString();
256 | qint64 size=static_cast(json["size"].toDouble());
257 | qint64 fileid=static_cast(json["fileid"].toDouble());
258 | addfilequeue(fileid,type,size,friendid,friendname);
259 | }else if(type=="sendmsg"){
260 | int fromid=json["fromid"].toInt();
261 | QString msg=json["msg"].toString();
262 | emit receivesendmsg(fromid,msg);
263 | }else if(type=="sendfile"){
264 | int fromid=json["fromid"].toInt();
265 | int toid=json["toid"].toInt();
266 | QString filename=json["filename"].toString();
267 | QString suffix=json["suffix"].toString();
268 | qint64 size=static_cast(json["size"].toDouble());
269 | qint64 fileid=static_cast(json["fileid"].toDouble());
270 | QString path="chat/"+QString::number(fromid)+"to"+QString::number(toid)+filename;
271 | QFile file(path);
272 | file.open(QIODevice::WriteOnly);
273 | file.close();
274 | emit frinedsendfile(size,filename,suffix,fromid);
275 | addfilequeue(fileid,type,size,0,"",path);
276 | }else if(type=="sendpicture"){
277 | int fromid=json["fromid"].toInt();
278 | int h=json["h"].toInt();
279 | int w=json["w"].toInt();
280 | qint64 size=static_cast(json["size"].toDouble());
281 | qint64 fileid=static_cast(json["fileid"].toDouble());
282 | addfilequeue(fileid,type,size,fromid,"","",h,w);
283 | }else if(type=="friendpicturechanged"){
284 | qDebug()<<"收到更换头像信号"<<'\n';
285 | int friendid=json["friendid"].toInt();
286 | qint64 size=static_cast(json["size"].toDouble());
287 | qint64 fileid=static_cast(json["fileid"].toDouble());
288 | addfilequeue(fileid,type,size,friendid);
289 | }else if(type=="friendnamechanged"){
290 | int friendid=json["friendid"].toInt();
291 | QString friendnewname=json["friendnewname"].toString();
292 | emit friendnamechanged(friendid,friendnewname);
293 | }
294 | }
295 |
296 | void SendData::addfilequeue(qint64 fileid, QString type,qint64 size,int friendid,QString friendname,QString path,int h,int w)
297 | {
298 | qDebug()<<"json处理"<<'\n';
299 | FileTransferContext newctx;
300 | newctx.fileId=fileid;
301 | newctx.totalSize=size;
302 | newctx.type=type;
303 | newctx.receivedSize=0;
304 | newctx.data.reserve(size);
305 | newctx.friendid=friendid;
306 | newctx.friendname=friendname;
307 | newctx.h=h;
308 | newctx.w=w;
309 |
310 | if(type=="sendfile"){
311 | newctx.path=path;
312 | }
313 | BeforFiledata *filedata=NULL;
314 | beforfilemutex.lock();
315 | filedata=selectfileid(fileid);
316 | while(filedata!=NULL){
317 | if(newctx.type=="sendfile"){
318 | QFile file(newctx.path);
319 | file.open(QIODevice::WriteOnly|QIODevice::Append);
320 | file.write(filedata->data);
321 | newctx.receivedSize+=filedata->data.size();
322 | }else{
323 | newctx.receivedSize+=filedata->data.size();
324 | newctx.data.append(filedata->data);
325 | }
326 | beforfilequeue.removeAll(*filedata);
327 | filedata=selectfileid(fileid);
328 | }
329 | beforfilemutex.unlock();
330 | qDebug()<<"fileid:"<=newctx.totalSize){
335 | if(newctx.type=="logon"){
336 | emit logonsuccseeicon(newctx.data);
337 | }else if(newctx.type=="login"){
338 | emit loginsuccseeicon(newctx.data);
339 | }else if(newctx.type=="initfriend"||newctx.type=="agreeadd"){
340 | emit initfriend(newctx.friendid,newctx.friendname,newctx.data);
341 | }else if(newctx.type=="selectuser"){
342 | emit showselectuser(newctx.friendid,newctx.friendname,newctx.data);
343 | }else if(newctx.type=="friendrequest"){
344 | emit showfriendrequest(newctx.friendid,newctx.friendname,newctx.data);
345 | }else if(newctx.type=="sendfile"){
346 | emit friendsendfilesize((int)(newctx.receivedSize*100/newctx.totalSize));
347 | }else if(newctx.type=="sendpicture"){
348 | emit friendsendpicture(newctx.friendid,newctx.data,newctx.h,newctx.w);
349 | }else if(newctx.type=="friendpicturechanged"){
350 | emit friendpicturechanged(newctx.friendid,newctx.data);
351 | }
352 | }else{
353 | qDebug()<<"加入任务队列"<<'\n';
354 | filemutex.lock();
355 | filequeue.enqueue(newctx);
356 | filemutex.unlock();
357 | }
358 | }
359 |
360 | BeforFiledata* SendData::selectfileid(qint64 fileid)
361 | {
362 | for(auto &other:beforfilequeue){
363 | if(other.fileid==fileid){
364 | return &other;
365 | }
366 | }
367 | return NULL;
368 | }
369 |
370 |
371 |
372 | void SendData::on_userlogon(int id, QString password)
373 | {
374 | QJsonObject json;
375 | json["type"]="logon";
376 | json["mark"]=mark;
377 | json["id"]=id;
378 | json["password"]=password;
379 | QJsonDocument doc(json);
380 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
381 | packingjson(m_data);
382 | }
383 |
384 | void SendData::on_login(QString name, QString password)
385 | {
386 | qDebug()<<"注册"<<'\n';
387 | QJsonObject json;
388 | json["type"]="login";
389 | json["mark"]=mark;
390 | json["name"]=name;
391 | json["password"]=password;
392 | QJsonDocument doc(json);
393 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
394 | packingjson(m_data);
395 | }
396 |
397 | void SendData::on_updateinformation(int id, QString name, QString path)//更新信息
398 | {
399 | QJsonObject json;
400 | json["type"]="updateinformation";
401 | json["name"]=name;
402 | json["id"]=id;
403 | QFile file(path);
404 | file.open(QIODevice::ReadOnly);
405 | qint64 size=file.size();
406 | qint64 fileId = QDateTime::currentMSecsSinceEpoch();
407 | json["fileid"]=fileId;
408 | json["size"]=size;
409 | QJsonDocument doc(json);
410 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
411 | packingjson(m_data);
412 |
413 | const qint64 CHUNK_SIZE = 1024 * 50;
414 | while(!file.atEnd()){
415 | QByteArray chunk=file.read(CHUNK_SIZE);
416 | QByteArray header;
417 | qint64 filemark = qToBigEndian(1);
418 | header.append(reinterpret_cast(&filemark), sizeof(filemark));
419 | qint64 m_fileid=qToBigEndian(fileId);
420 | header.append(reinterpret_cast(&m_fileid), sizeof(m_fileid));
421 | qint64 m_id=qToBigEndian(id);
422 | header.append(reinterpret_cast(&m_id), sizeof(m_id));
423 | qint64 len=qToBigEndian(chunk.size());
424 | header.append(reinterpret_cast(&len), sizeof(len));
425 | header.append(chunk);
426 | filesocket->write(header);
427 | }
428 | file.close();
429 | }
430 |
431 | void SendData::on_updatenameonly(int id, QString name)
432 | {
433 | QJsonObject json;
434 | json["type"]="updatenameonly";
435 | json["id"]=id;
436 | json["name"]=name;
437 | QJsonDocument doc(json);
438 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
439 | packingjson(m_data);
440 | }
441 |
442 |
443 |
444 | void SendData::on_selectfriendid(int selectid)
445 | {
446 | QJsonObject json;
447 | json["type"]="selectuser";
448 | json["id"]=id;
449 | json["selectid"]=selectid;
450 | json["mark"]=mark;
451 | QJsonDocument doc(json);
452 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
453 | packingjson(m_data);
454 | }
455 |
456 |
457 |
458 | void SendData::on_addfriendrequest(int toid)
459 | {
460 | QJsonObject json;
461 | json["type"]="friendrequest";
462 | json["fromid"]=this->id;
463 | json["toid"]=toid;
464 | json["fromname"]=this->name;
465 | QJsonDocument doc(json);
466 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
467 | packingjson(m_data);
468 | }
469 |
470 |
471 |
472 | void SendData::on_agreeaddfriend(int addid)
473 | {
474 | QJsonObject json;
475 | json["type"]="agreeadd";
476 | json["fromid"]=this->id;
477 | json["fromname"]=this->name;
478 | json["toid"]=addid;
479 | QJsonDocument doc(json);
480 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
481 | packingjson(m_data);
482 | }
483 |
484 |
485 |
486 | void SendData::on_sendtofriend(int toid, QString msg, int fromid)
487 | {
488 | QJsonObject json;
489 | json["type"]="sendmsg";
490 | json["fromid"]=fromid;
491 | json["msg"]=msg;
492 | json["toid"]=toid;
493 | QJsonDocument doc(json);
494 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
495 | packingjson(m_data);
496 | }
497 |
498 |
499 |
500 | void SendData::on_sendfiletofriend(int toid,QString path)
501 | {
502 | QJsonObject json;
503 | json["type"]="sendfile";
504 | json["fromid"]=this->id;
505 | json["toid"]=toid;
506 | QFileInfo fileinfo(path);
507 | json["filename"]=fileinfo.fileName();
508 | json["suffix"]=fileinfo.suffix().toLower();
509 | QFile file(path);
510 | file.open(QIODevice::ReadOnly);
511 | qint64 size=file.size();
512 | qint64 fileId = QDateTime::currentMSecsSinceEpoch();
513 | json["fileid"]=fileId;
514 | json["size"]=size;
515 | QJsonDocument doc(json);
516 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
517 | packingjson(m_data);
518 | qint64 sendsize=0;
519 | const qint64 CHUNK_SIZE = 1024 * 50;
520 | while(!file.atEnd()){
521 | QByteArray chunk=file.read(CHUNK_SIZE);
522 | sendsize+=chunk.size();
523 | static int lastProgress = -1;
524 | int currentProgress = (int)(sendsize * 100 / size);
525 | if (currentProgress != lastProgress) {
526 | emit mysendsize(currentProgress);
527 | lastProgress = currentProgress;
528 | }
529 | QByteArray header;
530 | qint64 filemark = qToBigEndian(2);
531 | header.append(reinterpret_cast(&filemark), sizeof(filemark));
532 | qint64 m_id=qToBigEndian(toid);
533 | header.append(reinterpret_cast(&m_id), sizeof(m_id));
534 | qint64 m_fileid=qToBigEndian(fileId);
535 | header.append(reinterpret_cast(&m_fileid), sizeof(m_fileid));
536 |
537 | qint64 len=qToBigEndian(chunk.size());
538 | header.append(reinterpret_cast(&len), sizeof(len));
539 | header.append(chunk);
540 | filesocket->write(header);
541 | QCoreApplication::processEvents();//发送大文件时让子线程有时间去处理msgsocket的消息
542 | QThread::usleep(100);//防止发送大文件时导致主线程卡死
543 | }
544 | file.close();
545 | }
546 |
547 | void SendData::on_sendpicture(QString path,int toid)
548 | {
549 | QJsonObject json;
550 | json["type"]="sendpicture";
551 | json["fromid"]=this->id;
552 | json["toid"]=toid;
553 | QPixmap pix(path);
554 | json["h"]=pix.height();
555 | json["w"]=pix.width();
556 | QFile file(path);
557 | file.open(QIODevice::ReadOnly);
558 | qint64 size=file.size();
559 | qint64 fileId = QDateTime::currentMSecsSinceEpoch();
560 | json["fileid"]=fileId;
561 | json["size"]=size;
562 | QJsonDocument doc(json);
563 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
564 | packingjson(m_data);
565 |
566 | const qint64 CHUNK_SIZE = 1024 * 50;
567 | while(!file.atEnd()){
568 | QByteArray chunk=file.read(CHUNK_SIZE);
569 | QByteArray header;
570 | qint64 filemark = qToBigEndian(2);
571 | header.append(reinterpret_cast(&filemark), sizeof(filemark));
572 | qint64 m_id=qToBigEndian(toid);
573 | header.append(reinterpret_cast(&m_id), sizeof(m_id));
574 | qint64 m_fileid=qToBigEndian(fileId);
575 | header.append(reinterpret_cast(&m_fileid), sizeof(m_fileid));
576 | qint64 len=qToBigEndian(chunk.size());
577 | header.append(reinterpret_cast(&len), sizeof(len));
578 | header.append(chunk);
579 | filesocket->write(header);
580 | }
581 | file.close();
582 | }
583 |
584 | void SendData::on_closehome()
585 | {
586 | QJsonObject json;
587 | json["type"]="close";
588 | json["mark"]=mark;
589 | json["id"]=this->id;
590 | QJsonDocument doc(json);
591 | QByteArray m_data = doc.toJson(QJsonDocument::Compact);
592 | packingjson(m_data);
593 | }
594 |
595 |
--------------------------------------------------------------------------------
/Client/senddata.h:
--------------------------------------------------------------------------------
1 | #ifndef SENDDATA_H
2 | #define SENDDATA_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | class SendData : public QObject
16 | {
17 | Q_OBJECT
18 | public:
19 | explicit SendData(QObject *parent = nullptr);
20 |
21 | private:
22 | QUdpSocket *udpsocket=NULL;
23 | QTcpSocket *msgsocket=NULL;
24 | QTcpSocket *filesocket=NULL;
25 | QQueuemsgqueue;
26 | QMutex msgmutex;
27 | QQueue filequeue;
28 | QQueuebeforfilequeue;
29 | QMutex beforfilemutex;
30 | QMutex filemutex;
31 | QString mark;
32 | int id;
33 | QString name;
34 | QPixmap profilephoto;
35 |
36 | private:
37 | void getudpsocket();
38 | void connectToServer(QString ip,int port);
39 | void packingjson(const QByteArray &m_data);
40 | Q_INVOKABLE void handleMessages();
41 | void processJson(const QJsonObject &json);
42 | void addfilequeue(qint64 fileid,QString type,qint64 size,int friendid=0,QString friendname="",QString path="",int h=0,int w=0);
43 | BeforFiledata* selectfileid(qint64 fileid);
44 |
45 | public:
46 | void init();
47 |
48 | private slots:
49 | void on_msgreadyRead();
50 | void on_filereadyRead();
51 |
52 |
53 | public slots:
54 | void on_userlogon(int id,QString password);
55 | void on_login(QString name,QString password);
56 | void on_updateinformation(int id,QString name,QString path);
57 | void on_updatenameonly(int id,QString name);
58 | void on_selectfriendid(int id);
59 | void on_addfriendrequest(int toid);
60 | void on_agreeaddfriend(int addid);
61 | void on_sendtofriend(int toid,QString msg,int fromid);
62 | void on_sendfiletofriend(int toid,QString path);
63 | void on_sendpicture(QString path,int toid);
64 | void on_closehome();
65 |
66 | public:signals:
67 | void logonsuccsee(int id,QString name);
68 | void logonfailure();
69 | void logonsuccseeicon(QByteArray icon);
70 | void loginsuccsee(int id,QString name);
71 | void loginfailure();
72 | void loginsuccseeicon(QByteArray icon);
73 | void initfriend(int id,QString name,QByteArray data);
74 | void isnullid();
75 | void showselectuser(int id,QString name,QByteArray data);
76 | void showfriendrequest(int id,QString name,QByteArray data);
77 | void receivesendmsg(int fromid,QString msg);
78 | void mysendsize(int value);
79 | void friendsendfilesize(int value);
80 | void frinedsendfile(qint64 size,QString filename,QString suffix,int fromid);
81 | void friendsendpicture(int fromid,QByteArray data,int h,int w);
82 | void deletesocket();
83 | void logoning();
84 | void friendpicturechanged(int friendid,QByteArray data);
85 | void friendnamechanged(int friendid,QString friendname);
86 | };
87 |
88 | #endif // SENDDATA_H
89 |
--------------------------------------------------------------------------------
/Client/stickerwidget.cpp:
--------------------------------------------------------------------------------
1 | #include "stickerwidget.h"
2 | #include "ui_stickerwidget.h"
3 | #include
4 |
5 | Stickerwidget::Stickerwidget(QWidget *parent)
6 | : QWidget(parent)
7 | , ui(new Ui::Stickerwidget)
8 | {
9 | ui->setupUi(this);
10 | this->setAttribute(Qt::WA_TranslucentBackground, true);
11 | //设置无边框
12 | this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
13 | //实例阴影shadow
14 | QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
15 | //设置阴影距离
16 | shadow->setOffset(0, 0);
17 | //设置阴影颜色
18 | shadow->setColor(QColor("#444444"));
19 | //设置阴影圆角
20 | shadow->setBlurRadius(30);
21 | //给嵌套QWidget设置阴影
22 | ui->widget->setGraphicsEffect(shadow);
23 | }
24 |
25 | Stickerwidget::~Stickerwidget()
26 | {
27 | delete ui;
28 | }
29 |
30 | void Stickerwidget::leaveEvent(QEvent *event)
31 | {
32 | emit focusOut();
33 | this->close();
34 | QWidget::leaveEvent(event);
35 | }
36 |
37 |
38 |
39 | void Stickerwidget::on_pushButton_clicked()
40 | {
41 | emit sticker1();
42 | }
43 |
44 |
45 | void Stickerwidget::on_pushButton_2_clicked()
46 | {
47 | emit sticker2();
48 | }
49 |
50 |
51 | void Stickerwidget::on_pushButton_3_clicked()
52 | {
53 | emit sticker3();
54 | }
55 |
56 |
57 | void Stickerwidget::on_pushButton_6_clicked()
58 | {
59 | emit sticker4();
60 | }
61 |
62 |
63 | void Stickerwidget::on_pushButton_7_clicked()
64 | {
65 | emit sticker5();
66 | }
67 |
68 |
69 | void Stickerwidget::on_pushButton_8_clicked()
70 | {
71 | emit sticker6();
72 | }
73 |
74 |
--------------------------------------------------------------------------------
/Client/stickerwidget.h:
--------------------------------------------------------------------------------
1 | #ifndef STICKERWIDGET_H
2 | #define STICKERWIDGET_H
3 |
4 | #include
5 |
6 | namespace Ui {
7 | class Stickerwidget;
8 | }
9 |
10 | class Stickerwidget : public QWidget
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | explicit Stickerwidget(QWidget *parent = nullptr);
16 | ~Stickerwidget();
17 |
18 | protected:
19 | void leaveEvent(QEvent *event) override;
20 |
21 | private slots:
22 | void on_pushButton_clicked();
23 |
24 | void on_pushButton_2_clicked();
25 |
26 | void on_pushButton_3_clicked();
27 |
28 | void on_pushButton_6_clicked();
29 |
30 | void on_pushButton_7_clicked();
31 |
32 | void on_pushButton_8_clicked();
33 |
34 | private:
35 | Ui::Stickerwidget *ui;
36 |
37 | public:signals:
38 | void focusOut();
39 | void sticker1();
40 | void sticker2();
41 | void sticker3();
42 | void sticker4();
43 | void sticker5();
44 | void sticker6();
45 | };
46 |
47 | #endif // STICKERWIDGET_H
48 |
--------------------------------------------------------------------------------
/Client/stickerwidget.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | Stickerwidget
4 |
5 |
6 |
7 | 0
8 | 0
9 | 169
10 | 94
11 |
12 |
13 |
14 | Form
15 |
16 |
17 | -
18 |
19 |
20 | border-radius:6px;
21 |
22 |
23 |
-
24 |
25 |
26 |
27 | 20
28 | 20
29 |
30 |
31 |
32 |
33 | 20
34 | 20
35 |
36 |
37 |
38 | background-color: rgba(255, 255, 255, 0);
39 |
40 |
41 |
42 |
43 |
44 |
45 | :/icon/35.png:/icon/35.png
46 |
47 |
48 |
49 | 20
50 | 20
51 |
52 |
53 |
54 |
55 | -
56 |
57 |
58 |
59 | 20
60 | 20
61 |
62 |
63 |
64 |
65 | 20
66 | 20
67 |
68 |
69 |
70 | background-color: rgba(255, 255, 255, 0);
71 |
72 |
73 |
74 |
75 |
76 |
77 | :/icon/36.png:/icon/36.png
78 |
79 |
80 |
81 | 20
82 | 20
83 |
84 |
85 |
86 |
87 | -
88 |
89 |
90 |
91 | 20
92 | 20
93 |
94 |
95 |
96 |
97 | 20
98 | 20
99 |
100 |
101 |
102 | background-color: rgba(255, 255, 255, 0);
103 |
104 |
105 |
106 |
107 |
108 |
109 | :/icon/37.png:/icon/37.png
110 |
111 |
112 |
113 | 20
114 | 20
115 |
116 |
117 |
118 |
119 | -
120 |
121 |
122 |
123 | 20
124 | 20
125 |
126 |
127 |
128 |
129 | 20
130 | 20
131 |
132 |
133 |
134 | background-color: rgba(255, 255, 255, 0);
135 |
136 |
137 |
138 |
139 |
140 |
141 | :/icon/38.png:/icon/38.png
142 |
143 |
144 |
145 | 20
146 | 20
147 |
148 |
149 |
150 |
151 | -
152 |
153 |
154 |
155 | 20
156 | 20
157 |
158 |
159 |
160 |
161 | 20
162 | 20
163 |
164 |
165 |
166 | background-color: rgba(255, 255, 255, 0);
167 |
168 |
169 |
170 |
171 |
172 |
173 | :/icon/41.png:/icon/41.png
174 |
175 |
176 |
177 | 20
178 | 20
179 |
180 |
181 |
182 |
183 | -
184 |
185 |
186 |
187 | 20
188 | 20
189 |
190 |
191 |
192 |
193 | 20
194 | 20
195 |
196 |
197 |
198 | background-color: rgba(255, 255, 255, 0);
199 |
200 |
201 |
202 |
203 |
204 |
205 | :/icon/40.png:/icon/40.png
206 |
207 |
208 |
209 | 20
210 | 20
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
--------------------------------------------------------------------------------
/MySQL/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/MySQL/friendships.ibd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/MySQL/friendships.ibd
--------------------------------------------------------------------------------
/MySQL/users.ibd:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/MySQL/users.ibd
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Chat1
2 | 使用代码之前要做点准备工作,首先要把MySQL文件中的两个表导入到MySQL中的test库中(不导入也没关系,只要在test库中创建那两个表了就行),然后先编辑一遍客户端与服务器,之后把客户端文件夹中的chat文件移动到客户端的项目目录下(发送与接收的文件都会记录在这个文件夹中),再把服务器文件夹中的usericon文件夹移动到服务器的项目目录下(这个文件夹里保存着用户的头像),之后就可以使用了。
3 | 两个表的结构如下
4 | 
5 |
6 | 
7 |
--------------------------------------------------------------------------------
/Server/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Server/FileTransferContext.h:
--------------------------------------------------------------------------------
1 | #ifndef FILETRANSFERCONTEXT_H
2 | #define FILETRANSFERCONTEXT_H
3 | #include
4 | class FileTransferContext{
5 | public:
6 | QString type;
7 | qint64 fileId;//文件唯一id
8 | qint64 totalSize;//文件大小
9 | qint64 receivedSize;//当前接收大小
10 | QByteArray data;//文件内容
11 |
12 | bool operator==(const FileTransferContext &other)const{
13 | return this->fileId==other.fileId;
14 | }
15 | };
16 |
17 | #endif // FILETRANSFERCONTEXT_H
18 |
--------------------------------------------------------------------------------
/Server/NewCharServer.pro:
--------------------------------------------------------------------------------
1 | QT += core gui
2 | QT+=network sql
3 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4 |
5 | CONFIG += c++17
6 |
7 | # You can make your code fail to compile if it uses deprecated APIs.
8 | # In order to do so, uncomment the following line.
9 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
10 |
11 | SOURCES += \
12 | linksql.cpp \
13 | main.cpp \
14 | mainwindow.cpp
15 |
16 | HEADERS += \
17 | FileTransferContext.h \
18 | linksql.h \
19 | mainwindow.h
20 |
21 | FORMS += \
22 | mainwindow.ui
23 |
24 | # Default rules for deployment.
25 | qnx: target.path = /tmp/$${TARGET}/bin
26 | else: unix:!android: target.path = /opt/$${TARGET}/bin
27 | !isEmpty(target.path): INSTALLS += target
28 |
--------------------------------------------------------------------------------
/Server/NewCharServer.pro.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | EnvironmentId
7 | {e0c42ccd-68bc-4fea-9627-6cbc6905b16b}
8 |
9 |
10 | ProjectExplorer.Project.ActiveTarget
11 | 0
12 |
13 |
14 | ProjectExplorer.Project.EditorSettings
15 |
16 | true
17 | false
18 | true
19 |
20 | Cpp
21 |
22 | CppGlobal
23 |
24 |
25 |
26 | QmlJS
27 |
28 | QmlJSGlobal
29 |
30 |
31 | 2
32 | UTF-8
33 | false
34 | 4
35 | false
36 | 0
37 | 80
38 | true
39 | true
40 | 1
41 | 0
42 | false
43 | true
44 | false
45 | 2
46 | true
47 | true
48 | 0
49 | 8
50 | true
51 | false
52 | 1
53 | true
54 | true
55 | true
56 | *.md, *.MD, Makefile
57 | false
58 | true
59 | true
60 |
61 |
62 |
63 | ProjectExplorer.Project.PluginSettings
64 |
65 |
66 | true
67 | false
68 | true
69 | true
70 | true
71 | true
72 |
73 | false
74 |
75 |
76 | 0
77 | true
78 |
79 | true
80 | true
81 | Builtin.DefaultTidyAndClazy
82 | 8
83 | true
84 |
85 |
86 |
87 | true
88 |
89 |
90 |
91 |
92 | ProjectExplorer.Project.Target.0
93 |
94 | Desktop
95 | Desktop Qt 6.5.3 MinGW 64-bit
96 | Desktop Qt 6.5.3 MinGW 64-bit
97 | qt.qt6.653.win64_mingw_kit
98 | 0
99 | 0
100 | 0
101 |
102 | 0
103 | F:\newChar\NewCharServer\build\Desktop_Qt_6_5_3_MinGW_64_bit-Debug
104 | F:/newChar/NewCharServer/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug
105 |
106 |
107 | true
108 | QtProjectManager.QMakeBuildStep
109 | false
110 |
111 |
112 |
113 | true
114 | Qt4ProjectManager.MakeStep
115 |
116 | 2
117 | 构建
118 | 构建
119 | ProjectExplorer.BuildSteps.Build
120 |
121 |
122 |
123 | true
124 | Qt4ProjectManager.MakeStep
125 | clean
126 |
127 | 1
128 | 清除
129 | 清除
130 | ProjectExplorer.BuildSteps.Clean
131 |
132 | 2
133 | false
134 |
135 | false
136 |
137 | Debug
138 | Qt4ProjectManager.Qt4BuildConfiguration
139 | 2
140 |
141 |
142 | F:\newChar\NewCharServer\build\Desktop_Qt_6_5_3_MinGW_64_bit-Release
143 | F:/newChar/NewCharServer/build/Desktop_Qt_6_5_3_MinGW_64_bit-Release
144 |
145 |
146 | true
147 | QtProjectManager.QMakeBuildStep
148 | false
149 |
150 |
151 |
152 | true
153 | Qt4ProjectManager.MakeStep
154 |
155 | 2
156 | 构建
157 | 构建
158 | ProjectExplorer.BuildSteps.Build
159 |
160 |
161 |
162 | true
163 | Qt4ProjectManager.MakeStep
164 | clean
165 |
166 | 1
167 | 清除
168 | 清除
169 | ProjectExplorer.BuildSteps.Clean
170 |
171 | 2
172 | false
173 |
174 | false
175 |
176 | Release
177 | Qt4ProjectManager.Qt4BuildConfiguration
178 | 0
179 | 0
180 |
181 |
182 | 0
183 | F:\newChar\NewCharServer\build\Desktop_Qt_6_5_3_MinGW_64_bit-Profile
184 | F:/newChar/NewCharServer/build/Desktop_Qt_6_5_3_MinGW_64_bit-Profile
185 |
186 |
187 | true
188 | QtProjectManager.QMakeBuildStep
189 | false
190 |
191 |
192 |
193 | true
194 | Qt4ProjectManager.MakeStep
195 |
196 | 2
197 | 构建
198 | 构建
199 | ProjectExplorer.BuildSteps.Build
200 |
201 |
202 |
203 | true
204 | Qt4ProjectManager.MakeStep
205 | clean
206 |
207 | 1
208 | 清除
209 | 清除
210 | ProjectExplorer.BuildSteps.Clean
211 |
212 | 2
213 | false
214 |
215 | false
216 |
217 | Profile
218 | Qt4ProjectManager.Qt4BuildConfiguration
219 | 0
220 | 0
221 | 0
222 |
223 | 3
224 |
225 |
226 | 0
227 | 部署
228 | 部署
229 | ProjectExplorer.BuildSteps.Deploy
230 |
231 | 1
232 |
233 | false
234 | ProjectExplorer.DefaultDeployConfiguration
235 |
236 | 1
237 |
238 | true
239 | true
240 | 0
241 | true
242 |
243 | 2
244 |
245 | false
246 | -e cpu-cycles --call-graph "dwarf,4096" -F 250
247 |
248 | Qt4ProjectManager.Qt4RunConfiguration:
249 | F:/newChar/NewCharServer/NewCharServer.pro
250 | false
251 | true
252 | true
253 | true
254 | F:/newChar/NewCharServer/build/Desktop_Qt_6_5_3_MinGW_64_bit-Debug
255 |
256 | 1
257 |
258 |
259 |
260 | ProjectExplorer.Project.TargetCount
261 | 1
262 |
263 |
264 | ProjectExplorer.Project.Updater.FileVersion
265 | 22
266 |
267 |
268 | Version
269 | 22
270 |
271 |
272 |
--------------------------------------------------------------------------------
/Server/linksql.cpp:
--------------------------------------------------------------------------------
1 | #include "linksql.h"
2 | #include
3 |
4 | LinkSQL::LinkSQL(QObject *parent)
5 | : QObject{parent}
6 | {
7 | sql = createConnection();
8 | if(!sql.open()){
9 | QMessageBox::warning(NULL,"注意","数据库连接错误");
10 | return;
11 | }else{
12 | qDebug()<<"打开成功"<<'\n';
13 | }
14 | }
15 |
16 |
17 | QSqlDatabase LinkSQL::createConnection()
18 | {
19 |
20 | QString connName = QString("conn_%1").arg(quintptr(QThread::currentThreadId()));//用线程地址生成唯一的连接id
21 |
22 | if (!QSqlDatabase::contains(connName)) {
23 | QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", connName);
24 | db.setHostName("localhost");
25 | db.setUserName("root");
26 | db.setPassword("20041203Li?");
27 | db.setDatabaseName("test");
28 | if (!db.open()) {
29 | qCritical() << "Failed to open database:" << db.lastError();
30 | return QSqlDatabase();
31 | }
32 | }
33 | return QSqlDatabase::database(connName);
34 | }
35 |
36 | bool LinkSQL::selectuser(int id, QString password)
37 | {
38 | QSqlQuery q(sql);
39 | q.prepare("select password from users where id=?;");
40 | q.addBindValue(id);
41 | q.exec();
42 | if(q.next()){
43 | QString spassword=q.value(0).toString();
44 | if(spassword==password){
45 | return 1;
46 | }
47 | }
48 | return 0;
49 | }
50 |
51 |
52 |
53 | QString LinkSQL::selecticon(int id)
54 | {
55 | QSqlQuery q(sql);
56 | q.prepare("select path from users where id=?;");
57 | q.addBindValue(id);
58 | q.exec();
59 | if(q.next()){
60 | return q.value(0).toString();
61 | }
62 | return "";
63 | }
64 |
65 | int LinkSQL::insertuser(QString name, QString password)
66 | {
67 | QSqlQuery q(sql);
68 | q.prepare("insert into users (username, password) values (?, ?)");
69 | q.addBindValue(name);
70 | q.addBindValue(password);
71 |
72 | if (!q.exec()) {
73 | qCritical() << "Insert failed:" << q.lastError();
74 | return 0;
75 | }
76 | QVariant idVar = q.lastInsertId();
77 | int newId = idVar.toInt();
78 | return newId;
79 | }
80 |
81 | void LinkSQL::updateinformation(int id, QString name, QString path)
82 | {
83 | QSqlQuery q(sql);
84 | q.prepare("update users set username=?,path=? where id=?");
85 | q.addBindValue(name);
86 | q.addBindValue(path);
87 | q.addBindValue(id);
88 | q.exec();
89 | }
90 |
91 | void LinkSQL::updatenameonly(int id, QString name)
92 | {
93 | QSqlQuery q(sql);
94 | q.prepare("update users set username=? where id=?");
95 | q.addBindValue(name);
96 | q.addBindValue(id);
97 | q.exec();
98 | }
99 |
100 |
101 |
102 | QString LinkSQL::selectname(int id)
103 | {
104 | QSqlQuery q(sql);
105 | q.prepare("select username from users where id=?;");
106 | q.addBindValue(id);
107 | q.exec();
108 | if(q.next()){
109 | return q.value(0).toString();
110 | }
111 | return "";
112 | }
113 |
114 |
115 |
116 | void LinkSQL::selectfriend(int id)
117 | {
118 | QSqlQuery q(sql);
119 | q.prepare("select friendid from friendships where userid=?");
120 | q.addBindValue(id);
121 | q.exec();
122 | while(q.next()){
123 | int friendid=q.value(0).toInt();
124 | QString friendname=selectname(friendid);
125 | qDebug()<<"id:"<
4 | #include
5 | #include
6 |
7 | class LinkSQL : public QObject
8 | {
9 | Q_OBJECT
10 | public:
11 | explicit LinkSQL(QObject *parent = nullptr);
12 |
13 | private:
14 | QSqlDatabase sql;
15 |
16 | private:
17 | static QSqlDatabase createConnection();
18 |
19 | public:
20 | bool selectuser(int id,QString password);
21 | QString selectname(int id);
22 | QString selecticon(int id);
23 | int insertuser(QString name,QString password);
24 | void updateinformation(int id,QString name,QString path);
25 | void updatenameonly(int id,QString name);
26 | void selectfriend(int id);
27 | void addfriend(int userid, int friendid);
28 |
29 | public:signals:
30 | void frienddata(int friendid,QString friendname);
31 | };
32 |
33 | #endif // LINKSQL_H
34 |
--------------------------------------------------------------------------------
/Server/main.cpp:
--------------------------------------------------------------------------------
1 | #include "mainwindow.h"
2 |
3 | #include
4 |
5 | int main(int argc, char *argv[])
6 | {
7 | QApplication a(argc, argv);
8 | MainWindow w;
9 | w.show();
10 | return a.exec();
11 | }
12 |
--------------------------------------------------------------------------------
/Server/mainwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef MAINWINDOW_H
2 | #define MAINWINDOW_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 |
17 | QT_BEGIN_NAMESPACE
18 | namespace Ui {
19 | class MainWindow;
20 | }
21 | QT_END_NAMESPACE
22 |
23 | class MainWindow : public QMainWindow
24 | {
25 | Q_OBJECT
26 |
27 | public:
28 | MainWindow(QWidget *parent = nullptr);
29 | ~MainWindow();
30 |
31 | private:
32 | struct A{
33 | QTcpSocket *msgsocket=NULL;
34 | QTcpSocket *filesocket=NULL;
35 | };
36 | struct B{
37 | QMutex *mutex=NULL;;
38 | QQueue* queue=NULL;
39 |
40 | B(){
41 | queue=new QQueue;
42 | mutex=new QMutex;
43 | }
44 | ~B(){
45 | delete mutex;
46 | mutex=NULL;
47 | delete queue;
48 | queue=NULL;
49 | }
50 | };
51 |
52 | unsigned int port=1210;
53 | QTcpServer *msgserver=NULL;
54 | QTcpServer *fileserver=NULL;
55 | QMapsocketmap;
56 | QMap*>queuemap;
57 | QMapmutexmap;
58 | QMapidmarkmap;
59 | QMutex mutexmutex;
60 | QMutex idmarkmutex;
61 | QMutex socketmapmutex;
62 | QMutex queuemapmutex;
63 | QString ip;
64 | QTimer *timer=NULL;
65 | qint64 m_fileid=0;
66 | QMutex fileidmutex;
67 | QMapofflinemsgmap;
68 | QMapvismap;
69 | QMutex vismapmutex;
70 | QMutex offlinemsgmapmutex;
71 |
72 | private:
73 | void managejson(QByteArray data,QTcpSocket *msgtcp);
74 | void packintjson(const QJsonObject &json,QTcpSocket *msgtcp);
75 | void packingfile(const QByteArray &chunk,qint64 fileid,QString mark,int toid);
76 | void addfilequeue(QQueue*queue,qint64 fileid,QString type,qint64 size,int id);
77 | void addfriend(QString type,QString mark,int userid,int friendid,QString friendname);
78 | void onlinesendmsg(int id);
79 | void sendonlinemsg(int id,QByteArray &data);
80 |
81 |
82 | private slots:
83 | void do_msgnewConnection();
84 | void do_filenewConnection();
85 | void msgreaddata(QTcpSocket *msgtcp);
86 | void filereaddata(QTcpSocket *filetcp);
87 |
88 | private:
89 | Ui::MainWindow *ui;
90 | };
91 | #endif // MAINWINDOW_H
92 |
--------------------------------------------------------------------------------
/Server/mainwindow.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | MainWindow
4 |
5 |
6 |
7 | 0
8 | 0
9 | 291
10 | 215
11 |
12 |
13 |
14 | MainWindow
15 |
16 |
17 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Server/usericon/...:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Server/usericon/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/1.png
--------------------------------------------------------------------------------
/Server/usericon/10018.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10018.png
--------------------------------------------------------------------------------
/Server/usericon/10022.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10022.png
--------------------------------------------------------------------------------
/Server/usericon/10023.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10023.png
--------------------------------------------------------------------------------
/Server/usericon/10045.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10045.png
--------------------------------------------------------------------------------
/Server/usericon/10046.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10046.png
--------------------------------------------------------------------------------
/Server/usericon/10047.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10047.png
--------------------------------------------------------------------------------
/Server/usericon/10048.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10048.png
--------------------------------------------------------------------------------
/Server/usericon/10049.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/2004Liy/Chat1/30c472b59e4e67437ead8e93446ef8755d4371ca/Server/usericon/10049.png
--------------------------------------------------------------------------------