#include <QApplication>#include "humanclient.h"int main(int argc, char **argv){ QApplication app(argc, argv); HumanClient client("localhost", 1516); client.show(); return app.exec();}------------------------------------------------#ifndef HUMANCLIENT_H#define HUMANCLIENT_H#include <QWidget>#include <QTcpSocket>#include "seamap.h"class QTextEdit;class QLineEdit;class HumanClient : public QWidget{Q_OBJECTprivate: QTcpSocket *tcpSocket; QTextEdit *textInfo; QLineEdit *textInput; quint16 nextBlockSize; SeaMap *firstMap; SeaMap *secondMap;public: HumanClient(const QString& host, int port, QWidget *parent = 0);signals:public slots: void slotReadyRead(); void slotError(QAbstractSocket::SocketError); void slotSendToCerver(); void slotConnected();};#endif // HUMANCLIENT_H----------------------------------------------#include <QtGui>#include <QtCore>#include <QtNetwork>#include "humanclient.h"HumanClient::HumanClient(const QString& host, int port, QWidget *parent) : QWidget(parent), nextBlockSize(0){ tcpSocket = new QTcpSocket(this); tcpSocket->connectToHost(host, port); connect(tcpSocket, SIGNAL(connected()), SLOT(slotConnected())); connect(tcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead())); connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotError(QAbstractSocket::SocketError))); textInfo = new QTextEdit; textInput = new QLineEdit; textInfo->setReadOnly(true);// QRegExp regExp("[A-J][0-9]");// textInput->setValidator(new QRegExpValidator(regExp, this)); firstMap = new SeaMap(this); secondMap = new SeaMap(this); QPushButton *sendButton = new QPushButton("Send"); connect(sendButton, SIGNAL(clicked()), SLOT(slotSendToCerver())); connect(textInput, SIGNAL(returnPressed()), this, SLOT(slotSendToCerver())); connect(firstMap, SIGNAL(seaCoord(const QString&)), textInput, SLOT(setText(const QString&))); connect(secondMap, SIGNAL(seaCoord(const QString&)), textInput, SLOT(setText(const QString&))); QVBoxLayout *vBoxLayout = new QVBoxLayout; vBoxLayout->addWidget(new QLabel("<H1>Client</H1>")); vBoxLayout->addWidget(textInfo); vBoxLayout->addWidget(textInput); vBoxLayout->addWidget(sendButton); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(firstMap); mainLayout->addWidget(secondMap); mainLayout->addLayout(vBoxLayout); setLayout(mainLayout);}void HumanClient::slotReadyRead(){ QDataStream input(tcpSocket); QString str; for (;;) { if (!nextBlockSize) { if (tcpSocket->bytesAvailable() < sizeof(quint16)) break; input >> nextBlockSize; } if (tcpSocket->bytesAvailable() < nextBlockSize) break; input >> str; textInfo->append(str); nextBlockSize = 0; }}void HumanClient::slotError(QAbstractSocket::SocketError error){ QString strError; strError = "Error: " + (error == QAbstractSocket::HostNotFoundError ? "The host not found." : error == QAbstractSocket::RemoteHostClosedError ? "The remote host is closed." : error == QAbstractSocket::ConnectionRefusedError ? "The connection was refused." : QString(tcpSocket->errorString())); textInfo->append(strError);}void HumanClient::slotSendToCerver(){ QByteArray block; QDataStream output(&block, QIODevice::WriteOnly); output << quint16(0) << textInput->text(); output.device()->seek(0); output << quint16(block.size() - sizeof(quint16)); tcpSocket->write(block); textInput->setText("");}void HumanClient::slotConnected(){ textInfo->append("Recieved the connected() signal");}------------------------------------------------#ifndef SEAMAP_H#define SEAMAP_H#include <QWidget>class SeaMap : public QWidget{Q_OBJECTprivate: quint8 **map;protected: void paintEvent (QPaintEvent *event); void mouseEvent (QMouseEvent *event);public: SeaMap(QWidget *parent = 0); ~SeaMap();signals: void seaCoord(const QString& str);};#endif // SEAMAP_H--------------------------------------#include "seamap.h"#include "qpainter.h"#include <QtGui/qevent.h>SeaMap::SeaMap(QWidget *parent) : QWidget(parent){ int i, j; map = new quint8*[10]; for (i = 0; i < 10; i++) { map[i] = new quint8[10]; for (j = 0; j < 10; j++) map[i][j] = 0; } setFixedSize(150, 150);}SeaMap::~SeaMap(){ for (int i = 0; i < 10; i++) delete []map[i]; delete []map;}void SeaMap::paintEvent (QPaintEvent* event){ QPainter paint(this); QBrush seaBrush = Qt::blue; QBrush brush = Qt::black; int i, j; for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) if (!map[i][j]) { paint.setBrush(seaBrush); paint.drawRect(i * 15, j * 15, 15, 15); } else { paint.setBrush(brush); paint.drawRect(i * 15, j * 15, 15, 15); }}//не работает, т.к. нажатие мыши на главном объектеvoid SeaMap::mouseEvent (QMouseEvent *event){ int x = event->pos().x() / 15; int y = event->pos().y() / 15; if (event->button() == Qt::LeftButton) { //QChar ch(x + 65); QString str = "qqq"; emit seaCoord(str); } update();}
C++ (Qt)void MouseObserver::mousePressEvent(QMouseEvent* event){ if (event->buttons() & Qt::RightButton) { //нажата правая }}
QRegExp rx("[A-J]([1-9]|[1][0])");QRegExpValidator *rxv = new QRegExpValidator(rx,this);ui->lineEdit->setValidator(rxv);