Qt 5.10 Book Examples
MyClient.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // MyClient.cpp
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtWidgets>
12 #include <QtGui>
13 #include "MyClient.h"
14 
15 // ----------------------------------------------------------------------
16 MyClient::MyClient(const QString& strHost,
17  int nPort,
18  QWidget* pwgt /*=0*/
19  ) : QWidget(pwgt)
20  , m_nNextBlockSize(0)
21 {
22  m_pTcpSocket = new QTcpSocket(this);
23 
24  m_pTcpSocket->connectToHost(strHost, nPort);
25  connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected()));
26  connect(m_pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
27  connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
28  this, SLOT(slotError(QAbstractSocket::SocketError))
29  );
30 
31  m_ptxtInfo = new QTextEdit;
32  m_ptxtInput = new QLineEdit;
33 
34  connect(m_ptxtInput, SIGNAL(returnPressed()),
35  this, SLOT(slotSendToServer())
36  );
37  m_ptxtInfo->setReadOnly(true);
38 
39  QPushButton* pcmd = new QPushButton("&Send");
40  connect(pcmd, SIGNAL(clicked()), SLOT(slotSendToServer()));
41 
42  //Layout setup
43  QVBoxLayout* pvbxLayout = new QVBoxLayout;
44  pvbxLayout->addWidget(new QLabel("<H1>Client</H1>"));
45  pvbxLayout->addWidget(m_ptxtInfo);
46  pvbxLayout->addWidget(m_ptxtInput);
47  pvbxLayout->addWidget(pcmd);
48  setLayout(pvbxLayout);
49 }
50 
51 // ----------------------------------------------------------------------
52 void MyClient::slotReadyRead()
53 {
54  QDataStream in(m_pTcpSocket);
55  in.setVersion(QDataStream::Qt_5_3);
56  for (;;) {
57  if (!m_nNextBlockSize) {
58  if (m_pTcpSocket->bytesAvailable() < (int)sizeof(quint16)) {
59  break;
60  }
61  in >> m_nNextBlockSize;
62  }
63 
64  if (m_pTcpSocket->bytesAvailable() < m_nNextBlockSize) {
65  break;
66  }
67  QTime time;
68  QString str;
69  in >> time >> str;
70 
71  m_ptxtInfo->append(time.toString() + " " + str);
72  m_nNextBlockSize = 0;
73  }
74 }
75 
76 // ----------------------------------------------------------------------
77 void MyClient::slotError(QAbstractSocket::SocketError err)
78 {
79  QString strError =
80  "Error: " + (err == QAbstractSocket::HostNotFoundError ?
81  "The host was not found." :
82  err == QAbstractSocket::RemoteHostClosedError ?
83  "The remote host is closed." :
84  err == QAbstractSocket::ConnectionRefusedError ?
85  "The connection was refused." :
86  QString(m_pTcpSocket->errorString())
87  );
88  m_ptxtInfo->append(strError);
89 }
90 
91 // ----------------------------------------------------------------------
92 void MyClient::slotSendToServer()
93 {
94  QByteArray arrBlock;
95  QDataStream out(&arrBlock, QIODevice::WriteOnly);
96  out.setVersion(QDataStream::Qt_5_3);
97  out << quint16(0) << QTime::currentTime() << m_ptxtInput->text();
98 
99  out.device()->seek(0);
100  out << quint16(arrBlock.size() - sizeof(quint16));
101 
102  m_pTcpSocket->write(arrBlock);
103  m_ptxtInput->setText("");
104 }
105 
106 // ------------------------------------------------------------------
107 void MyClient::slotConnected()
108 {
109  m_ptxtInfo->append("Received the connected() signal");
110 }
MyClient(const QString &strHost, int nPort, QWidget *pwgt=0)
Definition: MyClient.cpp:16