c++ - 连接客户端时如何在QT中更新QGraphicsView?

标签 c++ qt qt5

我在更新 QGraphicsView 时遇到问题。当我在类 myServer 中调用我的函数 void drawConnection() 时,我必须在其中更新我的 QGraphicsView 它不起作用,但在函数中我有 qDeubg() 并且正在调用。据我了解,问题不在函数调用中。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QtCore>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QEvent>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

static MainWindow* GetInstance(QWidget* parent = 0);

int matrix_[10][10];
int fire_[10][10];
int mylife = 10;

public:
Ui::MainWindow *ui;
static MainWindow* mainInstance;

QGraphicsScene *scene;
QGraphicsEllipseItem *ellipse;
QGraphicsRectItem *rectangle;
QGraphicsTextItem *text_;
QGraphicsTextItem *connection;
void mousePressEvent(QMouseEvent *ev);

public:
    //matrix
    void drawing();
    void createMatrix();
    void creatShip();
    //fire
    void createFireMatrix();
    void drawingFire();
    //life
    void drawingYourLife();
public:
    //conection
    void drawConnection();
    void drawDisConnection();
};

#endif // MAINWINDOW_H

主窗口.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow* MainWindow::mainInstance = 0;

MainWindow* MainWindow::GetInstance(QWidget *parent)
{
    if(mainInstance == NULL)
    {
        mainInstance = new MainWindow(parent);
    }
    return mainInstance;
}
void MainWindow::drawConnection()
{
    qDebug()<<"LOL";
    scene = new QGraphicsScene(this);
    ui->graphicsView_5->setScene(scene);
    QFont seriFont("Times",30,QFont::Bold);
    connection = scene->addText("Client is connecting",seriFont);
}

我的服务器.h

#ifndef MYSERVER_H
#define MYSERVER_H

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>

namespace Ui { class MainWindow; }

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

我的服务器.cpp

#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();
}

void myServer::sockDisc()
{
    qDebug()<<"DISCONECT";
    socket->deleteLater();
}

最佳答案

不要指出 qDebug() 你的意思是,在我的例子中,连接没有被调用。我还看到您正在实现不必要的方法,您可以查看以下示例:Fortune Server Example .

我还没有实现 incomingConnection() 方法,但我只使用了信号 newConnection 和方法 nextPendingConnection()

myserver.h

#ifndef MYSERVER_H
#define MYSERVER_H

#include "mainwindow.h"

#include <QTcpServer>

class MyServer : public QTcpServer
{
public:
    using QTcpServer::QTcpServer;
    void startServer();
private:
    MainWindow* instance = MainWindow::instance();
    void sockReady();
    void sockDisc();
};

#endif // MYSERVER_H

我的服务器.cpp

#include "myserver.h"

#include <QDebug>
#include <QTcpSocket>

void MyServer::startServer()
{
    if(listen(QHostAddress::Any,5555))
        qDebug()<<"LISTEN";
    else
        qDebug()<<"NOT LISTEN";
    connect(this,&QTcpServer::newConnection,this,&MyServer::sockReady);

}

void MyServer::sockReady()
{
    QTcpSocket *socket = nextPendingConnection();
    qDebug()<<"CONNECTED";
    instance->drawConnection();
    connect(socket, &QTcpSocket::disconnected, this, &MyServer::sockDisc);
}

void MyServer::sockDisc()
{
    QTcpSocket *socket = qobject_cast< QTcpSocket *>(sender());
    qDebug()<<"DISCONECT";
    if(socket)
        socket->deleteLater();
}

并且我已经重新排序了 MainWindow 代码。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QGraphicsTextItem;
class QGraphicsScene;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    static MainWindow *instance(QWidget* parent = 0);
    ~MainWindow();
    void drawConnection();

private:
    MainWindow(QWidget *parent = 0);
    Ui::MainWindow *ui;
    static MainWindow* mInstance;
    QGraphicsScene *scene;
    QGraphicsTextItem *connection;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QGraphicsScene>
#include <QGraphicsView>

MainWindow* MainWindow::mInstance = 0;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
}

MainWindow *MainWindow::instance(QWidget *parent)
{
    if(mInstance == NULL)
        mInstance = new MainWindow(parent);
    return mInstance;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::drawConnection()
{
    QFont seriFont("Times", 30, QFont::Bold);
    connection = scene->addText("Client is connecting",seriFont);
}

完整的例子可以在下面的link中找到.

关于c++ - 连接客户端时如何在QT中更新QGraphicsView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49326344/

相关文章:

c++ - QVariant 无法区分 QDateTime 和 QString

c++ - 在 main.cpp 之外创建一个类以使用 QtQuick2ApplicationViewer 的正确方法是什么?

c++ - 如何在控制台打印超过 ULLONG_MAX 的数字?

c++ - 在QT应用程序中制作一个透明孔

qt - 在 qmake 中运行 "source"bash shell 脚本

c++ - QT和QGLViewer程序中如何改变QGLViewer默认的远近裁剪距离?

c++ - 查询的SQL格式

c++ - 在QT中,如何在收到子部件产生的信号后从父部件中删除子部件?

c++ - 使用全局变量的 Xcode C++ 单元测试

c# - 编码 C# 枚举数组以在 C++ 代码中修改