简体   繁体   中英

Slots and signals in Qt C++ in QTcpServer app

There is a question about slots and signals in Qt C++ in QTcpServer app. I am not very familiar with slots and signals approach. So ...the issue is slots for client socket on server app is not being invoked at all. I think that I use connect function with wrong parametrs.

class CMyClient {
public:
    CMyClient();
    QTcpSocket* m_pClientSocket;
    MainWindow* m_pWin;
public slots:
    void onSocketReadyRead();
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketDisplayError(QAbstractSocket::SocketError);

I am using connect functions here:

void MainWindow::onNewConnection()
{
    CMyClient* pClient = new CMyClient();

    // set properties
    pClient->m_pClientSocket = m_pServSocket->nextPendingConnection();
    pClient->m_pWin = this;

    // events from client side on server side
    connect(pClient->m_pClientSocket, SIGNAL(readyRead()), SLOT(onSocketReadyRead()));
    connect(pClient->m_pClientSocket, SIGNAL(connected()), SLOT(onSocketConnected()));
    connect(pClient->m_pClientSocket, SIGNAL(disconnected()), SLOT(onSocketDisconnected()));
    connect(pClient->m_pClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onSocketDisplayError(QAbstractSocket::SocketError)));

... But these connect functions do not work properly. Clients are connecting, onNewConnection is invoked but events (slots) from client socket do not emerge (readyRead(), etc). Server are able to send messages to clients. Thanks.

In order to use the signals and slots the class must inherit from QObject , in your case CMyClient you must change it to something similar to:

.*h

class CMyClient: public QObject {
    Q_OBJECT
public:
    CMyClient(QObject *parent= 0);
    QTcpSocket* m_pClientSocket;
    MainWindow* m_pWin;
public slots:
    void onSocketReadyRead();
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketDisplayError(QAbstractSocket::SocketError);
};

.cpp

CMyClient::CMyClient(QObject *parent): QObject(parent){

}

According to the documentation :

connect ( const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)

connect ( const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type)

connect ( const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionTyp e type)

connect ( const QObject *sender, PointerToMemberFunction signal, Functor functor)

connect ( const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)

So in your case it is missing to place the receiver object.

CMyClient* pClient = new CMyClient(this);

connect(pClient->m_pClientSocket, SIGNAL(readyRead()), pClient, SLOT(onSocketReadyRead()));
connect(pClient->m_pClientSocket, SIGNAL(connected()), pClient, SLOT(onSocketConnected()));
connect(pClient->m_pClientSocket, SIGNAL(disconnected()), pClient, SLOT(onSocketDisconnected()));
connect(pClient->m_pClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), pClient, SLOT(onSocketDisplayError(QAbstractSocket::SocketError)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM