-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyclient.cpp
More file actions
148 lines (126 loc) · 4.42 KB
/
myclient.cpp
File metadata and controls
148 lines (126 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "myclient.h"
#include <QtWidgets>
#include <QStringList>
MyClient::MyClient(const QString &strHost, int nPort, QWidget *parent):
QWidget(parent),m_nNextBlockSize(0)
{
m_pTcpSocket = new QTcpSocket(this);
m_pTcpSocket->connectToHost(strHost,nPort);
connect(m_pTcpSocket,SIGNAL(connected()),SLOT(slotConnected()));
connect(m_pTcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),SLOT(slotError(QAbstractSocket::SocketError)));
connect(m_pTcpSocket,SIGNAL(readyRead()),SLOT(slotReadyRead()));
m_ptxtInput = new QLineEdit;
m_ptxtInfo = new QTextEdit;
m_ptable = new QTableWidget;
m_plbl = new QLabel;
//m_plbl->setScaledContents(true);
m_ptxtInfo->setReadOnly(true);
//prepare table to show
QStringList labels;
labels <<"Author" <<"Description" <<"Date" <<"Path";
m_ptable->setColumnCount(4);
m_ptable->setHorizontalHeaderLabels(labels);
int vwidth = m_ptable->verticalHeader()->width();
int hwidth = m_ptable->horizontalHeader()->length();
m_ptable->setMinimumWidth(vwidth + hwidth);
//add scrollarea for image
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(m_plbl);
scrollArea->setWidgetResizable(true);
scrollArea->setMinimumHeight(200);
scrollArea->setMinimumWidth(vwidth + hwidth);
QPushButton * pcmd = new QPushButton("&Send");
connect(pcmd,SIGNAL(clicked(bool)),SLOT(slotSendToServer()));
connect(m_ptxtInput,SIGNAL(returnPressed()),SLOT(slotSendToServer()));
QVBoxLayout * pvbxLayout = new QVBoxLayout;
pvbxLayout->addWidget(new QLabel("<h1>Client</h1>"));
pvbxLayout->addWidget(m_ptxtInfo);
pvbxLayout->addWidget(m_ptable);
pvbxLayout->addWidget(m_ptxtInput);
pvbxLayout->addWidget(scrollArea);
pvbxLayout->addWidget(pcmd);
setLayout(pvbxLayout);
}
void MyClient::slotReadyRead()
{
QDataStream in(m_pTcpSocket);
in.setVersion(QDataStream::Qt_5_5);
for(;;)
{
if(!m_nNextBlockSize){
if(m_pTcpSocket->bytesAvailable() < (qint64)sizeof(qint64))
break;
in >> m_nNextBlockSize;
}
if(m_pTcpSocket->bytesAvailable() < (qint64)m_nNextBlockSize)
{
break;
}
QString typemsg;
in >> typemsg;
QTime time;
in >> time;
if(typemsg == "data")
{
QString msg;
QVector<QStringList> v_rows;
//QImage img;
in>> msg >> v_rows;
if(!v_rows.isEmpty())
{
m_ptable->setRowCount(v_rows.size());
int index = 0;
foreach(QStringList str,v_rows)
{
m_ptable->setItem(index, 0, new QTableWidgetItem(str.at(0)));
m_ptable->setItem(index, 1, new QTableWidgetItem(str.at(1)));
m_ptable->setItem(index, 2, new QTableWidgetItem(str.at(2)));
m_ptable->setItem(index, 3, new QTableWidgetItem(str.at(3)));
++index;
}
}
}
else if(typemsg == "image")
{
QImage img;
in >> img;
m_plbl->setPixmap(QPixmap::fromImage(img));
// m_plbl->adjustSize();
}
m_ptxtInfo->append(time.toString());
m_nNextBlockSize = 0;
}
}
void MyClient::slotError(QAbstractSocket::SocketError err)
{
QString strError =
"Error:" +(err == QAbstractSocket::HostNotFoundError?
"The host was not found.":
err == QAbstractSocket::RemoteHostClosedError?
"The remote host is closed.":
err == QAbstractSocket::ConnectionRefusedError?
"The connection was refused.":
QString(m_pTcpSocket->errorString()));
m_ptxtInfo->append(strError);
}
void MyClient::slotSendToServer()
{
QByteArray arrBlock;
QDataStream out(&arrBlock,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_5);
out << quint64(0) << m_ptxtInput->text();
out.device()->seek(0);
qint64 size = arrBlock.size()-sizeof(quint64);
out<< quint64(size);
if(m_pTcpSocket->write(arrBlock) < size)
qWarning("transmit error");
m_ptxtInput->setText("");
}
void MyClient::slotConnected()
{
m_ptxtInfo->append("Received the connected signal()");
}
MyClient::~MyClient()
{
}