Не могу собрать клиента текстового чата из книги Макса Шлее Qt 4.8 Профессиональное программирование на С++. Может кто подскажет в чем проблема?
//main.cpp
#include <QApplication>
#include "MyClient.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyClient client("localhost", 2323);
client.show();
return app.exec();
}
// MyClient.cpp
#include <QTcpSocket>
#include "MyClient.h"
MyClient::MyClient(const QString& strHost,
int nPort,
QWidget* pwgt /*=0*/
) : QWidget(pwgt)
, m_nNextBlockSize(0)
{
m_pTcpSocket = new QTcpSocket(this);
m_pTcpSocket->connectToHost(strHost, nPort);
connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected()));
connect(m_pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(slotError(QAbstractSocket::SocketError))
);
m_ptxtInfo = new QTextEdit;
m_ptxtInput = new QLineEdit;
connect(m_ptxtInput, SIGNAL(returnPressed()),
this, SLOT(slotSendToServer())
);
m_ptxtInfo->setReadOnly(true);
QPushButton* pcmd = new QPushButton("&Send");
connect(pcmd, SIGNAL(clicked()), SLOT(slotSendToServer()));
//Layout setup
QVBoxLayout* pvbxLayout = new QVBoxLayout;
pvbxLayout->addWidget(new QLabel("<H1>Client</H1>"));
pvbxLayout->addWidget(m_ptxtInfo);
pvbxLayout->addWidget(m_ptxtInput);
pvbxLayout->addWidget(pcmd);
setLayout(pvbxLayout);
}
void MyClient::slotReadyRead()
{
QDataStream in(m_pTcpSocket);
in.setVersion(QDataStream::Qt_4_2);
for (;;) {
if (!m_nNextBlockSize) {
if (m_pTcpSocket->bytesAvailable() < sizeof(quint16)) {
break;
}
in >> m_nNextBlockSize;
}
if (m_pTcpSocket->bytesAvailable() < m_nNextBlockSize) {
break;
}
QTime time;
QString str;
in >> time >> str;
m_ptxtInfo->append(time.toString() + " " + str);
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_4_2);
out << quint16(0) << QTime::currentTime() << m_ptxtInput->text();
out.device()->seek(0);
out << quint16(arrBlock.size() - sizeof(quint16));
m_pTcpSocket->write(arrBlock);
m_ptxtInput->setText("");
}
void MyClient::slotConnected()
{
m_ptxtInfo->append("Received the connected() signal");
}
//MyClient.h
#ifndef _MyClient_h_
#define _MyClient_h_
#include <QWidget>
#include <QTcpSocket>
class QTextEdit;
class QLineEdit;
class MyClient : public QWidget {
Q_OBJECT
private:
QTcpSocket* m_pTcpSocket;
QTextEdit* m_ptxtInfo;
QLineEdit* m_ptxtInput;
quint16 m_nNextBlockSize;
public:
MyClient(const QString& strHost, int nPort, QWidget* pwgt = 0) ;
private slots:
void slotReadyRead ( );
void slotError (QAbstractSocket::SocketError);
void slotSendToServer( );
void slotConnected ( );
};
#endif //_MyClient_h_
//MyClient.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-09-10T09:50:56
#
#-------------------------------------------------
QT += core gui
QT += network widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyClient
TEMPLATE = app
SOURCES += main.cpp\
MyClient.cpp
HEADERS += MyClient.h
FORMS += MyClient.ui
Весь проект в архиве http://linuxdevelop.net/MyClient.zip