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

主窗口

#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

主窗口
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);
}

myserver.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

myserver.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

myserver.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代码。

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

mainwindow.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中找到。

10-01 17:20