Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MYSERVER_H
- #define MYSERVER_H
- #include <QMainWindow>
- #include <QTcpServer>
- #include <QTcpSocket>
- class myServer: public QTcpServer
- {
- Q_OBJECT
- public:
- myServer();
- ~myServer();
- public:
- QTcpSocket* socket;
- QByteArray Data;
- MainWindow* instance = MainWindow::GetInstance();
- public slots:
- void startServer();
- void incomingConnection(int socketDescriptor);
- void sockReady();
- void sockDisc();
- };
- #endif // MYSERVER_H
- #include "myserver.h"
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QWidget>
- #include <QGraphicsItem>
- myServer::myServer(){}
- myServer::~myServer(){}
- void myServer::startServer()
- {
- if(this->listen(QHostAddress::Any,5555))
- {
- qDebug()<<"LISTEN";
- }
- else{
- qDebug()<<"NOT LISTEN";
- }
- }
- void myServer::incomingConnection(int socketDescriptor)
- {
- socket = new QTcpSocket(this);
- socket->setSocketDescriptor(socketDescriptor);
- connect(socket,SIGNAL(readyRead()),this,SLOT(sockReady()));
- connect(socket,SIGNAL(disconnected()),this,SLOT(sockDisc()));
- qDebug()<<socketDescriptor<< "Client connected";
- socket->write("You are connect");
- qDebug()<<"Send client connect status - YES";
- }
- void myServer::sockReady()
- {
- instance->drawConnection();
- instance->drawingenemyLife();
- instance->show();
- }
- void myServer::sockDisc()
- {
- qDebug()<<"DISCONECT";
- instance->drawDisConnection();
- instance->show();
- socket->deleteLater();
- }
- void MainWindow::mousePressEvent(QMouseEvent *ev)
- {
- //Здесь надо вызвать socket->write("SOME TEXT");
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- ...
- signals:
- void messageCreated(const QString& str);
- ...
- };
- class myServer : public QObject
- {
- Q_OBJECT
- ...
- public slots:
- void slotSendMessage(const QString& str)
- {
- socket->write(str.toUtf8())
- }
- ...
- };
- void MainWindow::mousePressEvent(QMouseEvent* ev)
- {
- emit messageCreated("Very important message");
- }
- MainWindow w;
- myServer s;
- ...
- QObject::connect(&m, &MainWindow::messageCreated, &s, &myServer::slotSendMessage);
Add Comment
Please, Sign In to add comment